Temporal Trends in Philadelphia Mortality: The Impact of COVID-19, the Opioid Crisis, and Harm-Reduction Interventions, 2012–Present
BMIN5030 Final Project
Author
Michael McGarvey
How to push: git add . then git commit -m “my message” then git push ( This always should be in front: brb-05784:Final-Project-BMIN michaelmcgarvey$
1 Overview
Give a brief a description of your project and its goal(s), what data you are using to complete it, and what two faculty/staff in different fields you have spoken to about your project with a brief summary of what you learned from each person. Include a link to your final project GitHub repository.
This project investigates how Philadelphia’s mortality patterns have been shaped by two overlapping public health emergencies: the COVID-19 pandemic and the opioid crisis. Using cause-specific mortality data from 2012–2024, contextual metrics on COVID-19 pandemic onset, vaccination data, and the Introduction of fentanyl into the opioid supply and naloxone distribution, the study quantifies excess deaths. It evaluates the impact of these events and interventions across demographic groups.
Faculty guidance has further refined the project’s direction: Dr. Do emphasized the importance of hypothesis-driven analysis, encouraging the development and iterative refinement of testable questions, while Dr. Damrauer proposed framing the project as a natural experiment—examining not only the onset of the crises but also the timing and impact of key interventions such as vaccine rollout and Narcan availability. Their insights underscore the importance of modeling how mortality trends respond to policy shifts and to differences in resource distribution, particularly across age, race, and gender.
Describe the problem addressed, its significance, and some background to motivate the problem. This should extend what is in the ?@sec-overview.Explain why your problem is interdisciplinary, what fields can contribute to its understanding, and incorporate background related to what you learned from meeting with faculty/staff.
Philadelphia has experienced significant shifts in mortality patterns over the past decade, shaped by two overlapping public health emergencies: the COVID-19 pandemic and the opioid crisis. Mortality rates and causes changed during the initial years of the pandemic after 2020 and shifted again following 2014, when the potent synthetic opioid, fentanyl, began appearing in drug samples and overdose victims. A question of this project is whether these shifts in mortality reflect or intensify pre-existing disparities across age, race, and gender.
In response, the city launched targeted interventions to reduce mortality and mitigate harm. These included widespread COVID-19 vaccination beginning in late 2020 and expanded naloxone (Narcan) distribution, marked by key milestones: citywide distribution to first responders and community organizations in 2017, the installation of free Narcan vending machines in 2022, and over-the-counter availability in 2023. This project seeks to quantify changes in cause-specific mortality rates from 2012 onward and assess the extent to which these interventions influenced excess deaths.
The complexity of this problem demands an interdisciplinary approach. Public health and epidemiology provide frameworks for understanding disease burden and intervention strategies, biostatistics and informatics supply the tools to analyze large-scale mortality datasets, and social science highlights how structural inequities shape vulnerability to both infectious disease and substance use.
Faculty guidance from the informatics department further refined the project’s direction. Dr. Do emphasized the importance of hypothesis-driven analysis, encouraging the development and iterative refinement of testable questions. In response, I created a set of hypotheses examining how mortality trends shifted with the onset of crises and the rollout of interventions. Dr. Damrauer reinforced this approach by proposing that the project be framed as a natural experiment—analyzing not only the onset of COVID-19 and the introduction of fentanyl, but also the timing and impact of vaccine availability and naloxone distribution. Their insights underscore the importance of modeling how mortality trends respond to policy shifts and to differences in resource distribution, particularly across age, race, and gender.
3 Methods
Describe the data used and general methodological approach used to address the problem described in the Section 2. Subsequently, incorporate full R code necessary to retrieve and clean data, and perform analysis. Be sure to include a description of code so that others (including your future self) can understand what you are doing and why.
Data Sources This project draws on the Philadelphia Vital Statistics mortality database (2012–2024), which provides annual, aggregated mortality data for Philadelphia County. Each record summarizes deaths by cause, demographic group, and metric type. Key fields include year, sex, race/ethnicity, age categories, leading cause of death, and mortality metrics such as counts, age‑adjusted rates per 100,000, and life expectancy. Records flagged as unreliable (<20 deaths) were retained to preserve temporal trends, while suppressed values (<10 deaths) were excluded to ensure data quality.
To contextualize mortality patterns, additional datasets were merged with the Vital Statistics database. These included annual COVID‑19 vaccination coverage (2020–present), naloxone (Narcan) distribution milestones (2017 citywide rollout, 2022 vending machines, 2023 over‑the‑counter availability), and critical inflection points such as the emergence of fentanyl in the drug supply (2014) and the onset of the COVID‑19 pandemic (2020). These contextual variables provide anchors for evaluating how crises and interventions shaped mortality trajectories.
Load required R packages for data import, cleaning, and analysis
Data Cleaning and Structuring Data preparation involved several steps. Unused columns such as geography_name, geography, and estimate_type were removed to streamline the dataset. Suppressed values (metric_value == -99999 or quality_flag == “suppressed”) were excluded, while records flagged as “unreliable” were retained to preserve long‑term patterns. Demographic categories were standardized to ensure consistency across years. Finally, contextual datasets on vaccination coverage, naloxone milestones, fentanyl introduction, and pandemic onset were merged with mortality records to create a unified analytic dataset.
Analytical Approach The analysis proceeded in five stages. First, total mortality summaries were calculated for Philadelphia by year, with contextual overlays marking fentanyl introduction, pandemic onset, and intervention milestones. Second, interrupted time‑series (ITS) regression models were used to estimate level and slope changes associated with critical inflection points (2014 fentanyl emergence, 2020 pandemic onset) and intervention years (vaccination rollout, naloxone milestones). Third, cause‑of‑death analysis was conducted through interactive plots of the top 10 causes of death by year, accompanied by tables highlighting rank and percent contributions of COVID‑19 and drug overdose mortality. Fourth, demographic analysis explored disparities across sex, race/ethnicity, and age groups, using regression models (linear, Poisson, negative binomial) to quantify associations between mortality counts, demographic indicators, and interventions. Finally, hypothesis testing was performed to evaluate mortality trends, intervention impacts, and interactions, with full hypotheses documented to guide analysis and interpretation.
Hypotheses
Mortality Trends Hypotheses: Overall mortality rates increased after the onset of COVID‑19 (2020); opioid‑related mortality rose sharply after fentanyl emergence (2014); COVID‑19 deaths disproportionately affected older age groups; opioid‑related deaths disproportionately affected males; Black and Hispanic populations experienced higher excess mortality during COVID‑19 than white populations.
Intervention Impact Hypotheses: COVID‑19 mortality declined following vaccination rollout (late 2020 onward); opioid mortality declined after Narcan distribution to first responders (2017); free Narcan vending machines (2022) reduced overdose deaths; over‑the‑counter Narcan (2023) contributed to declines across demographic groups.
Interaction Hypotheses: Effectiveness of vaccination varied by race, age, and gender; Narcan’s impact was greater in younger age groups; mortality disparities narrowed following interventions, suggesting improved equity.
Specific Aims The overarching aim of this study is to evaluate how overlapping public health crises and subsequent interventions shaped mortality in Philadelphia between 2012 and 2024. Specifically, the analysis seeks to characterize annual trends in leading causes of death, calculate mortality counts and age‑standardized rates for major causes such as heart disease, cancer, overdose, and COVID‑19, and estimate the impact of crises on mortality trajectories. Interrupted time‑series regression is used to assess deviations in mortality patterns following the emergence of fentanyl in 2014 and the onset of the COVID‑19 pandemic in 2020. A further aim is to evaluate the moderating effects of interventions, including vaccination uptake and naloxone distribution, to determine whether these efforts reduced excess deaths and narrowed disparities across demographic groups. By integrating contextual data with mortality records, the study aims to provide a comprehensive assessment of how crises and interventions jointly influenced mortality trends in Philadelphia.
Data Preparation and Integration
Loading Packages and Preparing Mortality Data: To begin the analysis, I loaded the required R packages that support data wrangling, visualization, and statistical modeling. The tidyverse suite provided core tools for data manipulation and plotting, while packages such as janitorstandardized column names, readr enabled efficient CSV import, and MASS supported negative binomial regression. Additional packages like ggrepel and RColorBrewer improved plot readability and accessibility, and knitr and gt were used to generate clean, publication‑ready tables.
After setting up the environment, we imported the raw mortality dataset (Vital_Mortality_Cty‑2.csv), standardized its column names, and removed metadata fields not needed for analysis. Suppressed values (e.g., metric_value == ‑99999 or flagged as “suppressed”) were excluded, while unreliable records were retained to preserve long‑term trends. To avoid conflicts between packages, we explicitly called dplyr::select when dropping unused columns. Finally, we validated the import by checking column names and inspecting the dataset structure. These steps ensured that the dataset was tidy, consistent, and ready for downstream filtering, merging with contextual datasets, and modeling.
# Load required packageslibrary(tidyverse) # Core toolkit: dplyr for wrangling, ggplot2 for visualization
Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'readr' was built under R version 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.6
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.1 ✔ tibble 3.3.0
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.2.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(janitor) # clean_names() for standardized, consistent column names
Attaching package: 'janitor'
The following objects are masked from 'package:stats':
chisq.test, fisher.test
library(readr) # Fast, consistent CSV importlibrary(broom) # Tidies model outputs into data frameslibrary(ggrepel) # Improves readability of plot labelslibrary(RColorBrewer) # Accessible color palettes for plotslibrary(MASS) # Provides glm.nb() for Negative Binomial regression
Attaching package: 'MASS'
The following object is masked from 'package:dplyr':
select
library(knitr) # kable() for clean, simple tables in R Markdown/Quartolibrary(gt) # gt() for polished, publication-ready tableslibrary(stringr) # str_detect() and other string functions for filtering predictorslibrary(ggplot2) # Explicitly loaded for forest plots (part of tidyverse, noted here)library(patchwork)
Attaching package: 'patchwork'
The following object is masked from 'package:MASS':
area
select <- dplyr::select# Import mortality data, clean names, drop metadata that I will not use, and filter suppressed valuesmortality <-read_csv("Vital_Mortality_Cty-2.csv", skip =1, show_col_types =FALSE) %>%clean_names() %>%# Explicitly call dplyr::select to avoid masking errors from other packages dplyr::select(-geography_name, -geography, -estimate_type) %>%# Filter out suppressed or placeholder valuesfilter( metric_value !=-99999,is.na(quality_flag) | quality_flag !="suppressed" )# Quick validation of column names and structurenames(mortality)
Rows: 38,581
Columns: 10
$ objectid <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,…
$ year <dbl> 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 20…
$ sex <chr> "All sexes", "All sexes", "All sexes", "All sexes"…
$ race_ethnicity <chr> "All races/ethnicities", "All races/ethnicities", …
$ age_category <chr> "All ages", "All ages", "All ages", "All ages", "A…
$ leading_cause_death <chr> "All alcohol-attributable causes", "All alcohol-at…
$ metric_name <chr> "alcohol_attributable_deaths", "age_adjusted_alcoh…
$ metric_value <dbl> 502.503966, 30.814007, 3.707148, 16.017177, 11.154…
$ rank <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ quality_flag <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
Preparation of Contextual Datasets: In this step, two external contextual datasets—opioid interventions and COVID‑19 vaccination coverage—were prepared for integration with the mortality data. The primary goal was to standardize their schemas to ensure consistency across sources. For both datasets, the year column was converted to integer type to align with the mortality database and enable accurate joins. Because different versions of the source files stored notes under varying column names, a single, consistent notes column was enforced (opioid_notes for opioid interventions and vax_notes for vaccination coverage). This was achieved using rename_with() and matches() to harmonize column names in a compact, reproducible way. Safeguards were applied with coalesce() to guarantee that the notes column always exists, even if missing in the source file, thereby preventing downstream errors and maintaining transparency. Validation was performed by inspecting the dataset structure with glimpse() and confirming distinct years with distinct(year). These steps ensured that both contextual datasets were tidy, consistent, and ready to be merged with the cleaned mortality data for subsequent analyses.
# Contextual Datasets Incorporated# ---- Opioid crisis interventions ----opioid_interventions <-read_csv("intervention_data.csv", show_col_types =FALSE) %>%clean_names() %>%mutate(year =as.integer(year)) %>%# ensure year is numeric for merging# Standardize notes column across versionsrename_with(~"opioid_notes", matches("opioid_crisis_notes_philadelphia|notes")) %>%mutate(opioid_notes =coalesce(opioid_notes, NA_character_))# Validate structureglimpse(opioid_interventions)
# A tibble: 5 × 1
year
<int>
1 2020
2 2021
3 2022
4 2023
5 2024
Merging Mortality Data with Contextual Datasets: After cleaning and preparing the individual datasets, the next step was to merge the mortality records with contextual information on opioid interventions and COVID‑19 vaccination coverage. Defensive checks were applied to confirm that the year column was present in each dataset before merging, ensuring that joins would execute correctly. Using left_join() by year preserved all mortality observations while adding corresponding intervention and vaccination data where available. To prevent ambiguity in cases where datasets contained overlapping column names, explicit suffixes were applied during the joins, making the resulting variables easier to interpret in downstream analyses. Finally, the unified dataset was inspected with glimpse() to validate that the merges executed correctly and that the structure aligned with expectations. These refinements ensured that the merged dataset was both comprehensive and reliable, providing a solid foundation for subsequent analyses of mortality trends in relation to crises and interventions.
# ---- Merge mortality data with opioid interventions by year ----# Defensive check: ensure 'year' exists in both datasetsstopifnot("year"%in%names(mortality))stopifnot("year"%in%names(opioid_interventions))mortality_with_opioid <- mortality %>%left_join(opioid_interventions, by ="year", suffix =c("", "_opioid"))# ---- Merge the result with COVID-19 vaccination coverage by year ----# Defensive check: ensure 'year' exists in vaccination datasetstopifnot("year"%in%names(covid_vax))mortality_full <- mortality_with_opioid %>%left_join(covid_vax, by ="year", suffix =c("", "_vax"))# Preview the unified dataset to confirm joins worked correctlyglimpse(mortality_full)
Annual Mortality Trends: Data Preparation and Visualization: To summarize overall mortality patterns, the unified dataset (mortality_full) was filtered to include only rows representing all causes of death across all sexes, races/ethnicities, and age groups. Restricting the metric to raw death counts ensured that each year was represented by a single, clean value reflecting total mortality in Philadelphia. The year column was standardized as an integer, and death counts were renamed as total_deaths. Only relevant columns were retained using dplyr::select() (explicitly called to avoid function conflicts), and duplicates were removed to guarantee one row per year. Sanity checks confirmed the filtering worked correctly, leaving a tidy table of annual death totals. Finally, a line plot with points was generated to visualize trends in total deaths over time. To improve readability, large values on the y‑axis were formatted with commas, and a consistent plotting theme was applied to maintain clarity across figures. This visualization provides a clear descriptive overview of mortality patterns and establishes a foundation for subsequent regression modeling.
library(ggplot2)library(scales) # for comma formatting
Attaching package: 'scales'
The following object is masked from 'package:purrr':
discard
The following object is masked from 'package:readr':
col_factor
# Filter to the single "All causes" row per yearannual_totals <- mortality_full %>%filter( sex =="All sexes", race_ethnicity =="All races/ethnicities", age_category =="All ages", leading_cause_death =="All causes", metric_name =="count_of_deaths" ) %>%mutate(year =as.integer(year)) %>% dplyr::select(year, total_deaths = metric_value) %>%distinct() # Sanity checksannual_totals %>%count(year)
# A tibble: 0 × 3
# ℹ 3 variables: year <int>, total_deaths <dbl>, n <int>
# Plot total deaths per year with refinementsggplot(annual_totals, aes(x = year, y = total_deaths)) +geom_line(color ="steelblue", size =1.2) +geom_point(color ="darkred", size =2) +labs(title ="Total Deaths per Year in Philadelphia",subtitle ="All causes, all ages, sexes, and races/ethnicities",x ="Year",y ="Total Deaths" ) +scale_y_continuous(labels = comma) +# axis scaling for readabilitytheme_classic(base_size =14) # consistent theme across plots
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
Mortality Trends with Opiode Interventions and Vaccine Milestones: Building on the annual mortality totals, a line chart was created to display total deaths per year in Philadelphia. Points were added to highlight each year’s value, and vertical dashed lines were layered to mark major public health interventions. Opioid‑related events — including the fentanyl surge (2014), Narcan distribution (2017), vending machine rollout (2022), and OTC Narcan availability (2023) — were each assigned distinct colors to differentiate their timing and impact. In contrast, COVID‑related milestones were consistently marked in blue for visual coherence.
To enhance readability, the COVID onset year was labeled at the top of the plot, while vaccine rollout years were marked at the bottom with their actual dose counts. This design choice separates onset and rollout labels vertically, reducing clutter while still showing how both types of interventions align with mortality trends.
The resulting visualization provides a context‑rich overview that integrates mortality patterns with opioid and COVID responses, offering a clear depiction of how these public health milestones intersect with annual death counts in Philadelphia.
# Ensure dplyr verbs are used explicitly to avoid masking issueslibrary(ggplot2)library(dplyr)library(scales)# Join vaccine dose counts into annual_totalsannual_totals_with_vax <- annual_totals %>% dplyr::left_join( covid_vax %>% dplyr::select(year, covid_vaccine_doses_philadelphia),by ="year" )# Automatically detect the first year with vaccine doses (COVID onset year)covid_onset_year <- annual_totals_with_vax %>% dplyr::filter(!is.na(covid_vaccine_doses_philadelphia)) %>% dplyr::summarise(first_year =min(year)) %>% dplyr::pull(first_year)# Optional: quick checkprint(covid_onset_year)
# Plotggplot(annual_totals_with_vax, aes(x = year, y = total_deaths)) +# Base mortality trendgeom_line(color ="steelblue", size =1.2) +geom_point(color ="darkred", size =2) +# Opioid intervention milestonesgeom_vline(xintercept =2014, linetype ="dashed", color ="red") +geom_vline(xintercept =2017, linetype ="dashed", color ="purple") +geom_vline(xintercept =2022, linetype ="dashed", color ="darkgreen") +geom_vline(xintercept =2023, linetype ="dashed", color ="orange") +# COVID onset marker (auto-detected)geom_vline(xintercept = covid_onset_year, linetype ="dashed", color ="blue") +# Labels for interventionsannotate("text", x =2014, y =max(annual_totals_with_vax$total_deaths)*0.95,label ="Fentanyl surge", angle =90, vjust =-0.5, color ="red") +annotate("text", x =2017, y =max(annual_totals_with_vax$total_deaths)*0.95,label ="Narcan distribution", angle =90, vjust =-0.5, color ="purple") +annotate("text", x = covid_onset_year, y =max(annual_totals_with_vax$total_deaths)*0.92,label =paste("COVID onset (", covid_onset_year, ")", sep =""),angle =90, vjust =-0.5, color ="blue") +annotate("text", x =2022, y =max(annual_totals_with_vax$total_deaths)*0.95,label ="Narcan vending machines", angle =90, vjust =-0.5, color ="darkgreen") +annotate("text", x =2023, y =max(annual_totals_with_vax$total_deaths)*0.95,label ="OTC Narcan", angle =90, vjust =-0.5, color ="orange") +# Vaccine dose labelsgeom_text(data = annual_totals_with_vax %>% dplyr::filter(!is.na(covid_vaccine_doses_philadelphia)),aes(x = year,y =min(annual_totals_with_vax$total_deaths) *1.05,label =paste0("Vaccines: ", covid_vaccine_doses_philadelphia) ),angle =90, vjust =1, color ="blue", inherit.aes =FALSE ) +labs(title ="Total Deaths per Year in Philadelphia",subtitle ="All causes, all ages, sexes, and races/ethnicities\nIntervention milestones and vaccine dose counts marked",x ="Year",y ="Total Deaths" ) +theme_minimal()
4.1 Interrupted Time Series Analysis of Mortality Trends in Philadelphia
Interrupted Time Series of COVID-Specific Mortality in Philadelphia, 2012–2024: This ITS analysis examined annual COVID-19 deaths in Philadelphia across the full period 2012–2024, with deaths set to zero prior to 2020 to establish a baseline. Intervention milestones were defined as 2020 (pandemic onset), 2021 (initial vaccine rollout), and 2022 (booster rollout). Dummy indicators captured immediate level changes, slope variables measured shifts in trend, and a combined indicator represented the broader vaccine era beginning in 2021.
The observed data showed zero COVID deaths through 2019, followed by 2,460 deaths in 2020, 1,580 in 2021, 857 in 2022, 173 in 2023, and 94 in 2024. The regression results estimated a highly significant immediate increase in deaths at onset in 2020 (≈ 4,766, p < 0.001), followed by a significant negative slope (≈ –1,108 per year, p < 0.001), consistent with the observed sharp spike and subsequent decline. The 2021 vaccine rollout was associated with a modest negative level change (≈ –571, p ≈ 0.05), while the 2022 booster rollout had no significant independent effect (≈ 156, p ≈ 0.59). The combined vaccine-era indicator reinforced the broader downward adjustment in COVID deaths after 2021, though collinearity limited its independent estimation. Importantly, the model explained 99.2% of the variation in COVID deaths (adjusted R² = 0.986) with an average error of only about 93 deaths per year, and the overall regression was highly significant (F = 165, p < 0.000001).
The visualization displayed observed COVID deaths across all years 2012–2024 alongside fitted ITS values, with intervention milestones clearly marked. Extending the lines back to 2012 provided a full historical baseline, showing zero deaths through 2019, the sharp spike to 2,460 in 2020, and the subsequent decline during the vaccine era. This comprehensive view highlights both the actual mortality burden and the model-estimated disruptions, offering a clear depiction of how COVID-specific mortality evolved in Philadelphia over time.
library(dplyr)library(ggplot2)library(scales)library(broom)# Step 1: Extend dataset back to 2012 with zeros before 2020years_full <-tibble(year =2012:2024)covid_totals <- mortality_full %>% dplyr::filter( sex =="All sexes", race_ethnicity =="All races/ethnicities", age_category =="All ages", leading_cause_death =="COVID-19", metric_name =="count_of_deaths" ) %>%mutate(year =as.integer(year)) %>% dplyr::select(year, covid_deaths = metric_value) %>%distinct()covid_totals <- years_full %>%left_join(covid_totals, by ="year") %>%mutate(covid_deaths =ifelse(is.na(covid_deaths) & year <2020, 0, covid_deaths))# Step 2: Create ITS variablescovid_totals <- covid_totals %>%arrange(year) %>%mutate(time = year -2012+1,post_2020 =ifelse(year >=2020, 1, 0),time_post2020 =ifelse(year >=2020, time - (2020-2012+1) +1, 0),post_2021 =ifelse(year >=2021, 1, 0),time_post2021 =ifelse(year >=2021, time - (2021-2012+1) +1, 0),post_2022 =ifelse(year >=2022, 1, 0),time_post2022 =ifelse(year >=2022, time - (2022-2012+1) +1, 0),post_2021plus =ifelse(year >=2021, 1, 0) )# Step 3: Fit ITS regressioncovid_model_multi <-lm( covid_deaths ~ time + post_2020 + time_post2020 + post_2021 + time_post2021 + post_2022 + time_post2022 + post_2021plus,data = covid_totals)# Step 4: Add fitted valuescovid_totals$fitted <-predict(covid_model_multi)# Step 5: Plot observed vs fitted across 2012–2024ggplot(covid_totals, aes(x = year)) +geom_line(aes(y = covid_deaths), color ="darkred", size =1.2) +geom_point(aes(y = covid_deaths), color ="darkred", size =2) +geom_line(aes(y = fitted), color ="steelblue", linetype ="dashed", size =1.2) +geom_vline(xintercept =2020, linetype ="dashed", color ="blue") +geom_vline(xintercept =2021, linetype ="dashed", color ="blue") +geom_vline(xintercept =2022, linetype ="dashed", color ="blue") +annotate("text", x =2020, y =max(covid_totals$covid_deaths)*0.9,label ="COVID onset (2020)", angle =90, vjust =-0.5, color ="blue") +annotate("text", x =2021, y =max(covid_totals$covid_deaths)*0.9,label ="Vaccine rollout (2021)", angle =90, vjust =-0.5, color ="blue") +annotate("text", x =2022, y =max(covid_totals$covid_deaths)*0.9,label ="Booster rollout (2022)", angle =90, vjust =-0.5, color ="blue") +labs(title ="ITS: COVID-Specific Mortality Interventions in Philadelphia",subtitle ="Observed COVID deaths (red) vs fitted ITS model (blue dashed)\nAll years 2012–2024 included with intervention milestones marked",x ="Year",y ="COVID Deaths" ) +scale_x_continuous(breaks =2012:2024, limits =c(2012, 2024)) +scale_y_continuous(labels = comma) +theme_classic(base_size =14)
Interrupted Time Series of COVID-Specific Mortality in Philadelphia, Grouped Pandemic Era (2012–2024): This ITS analysis examined annual COVID-19 deaths in Philadelphia across the full period 2012–2024, with deaths set to zero prior to 2020 to establish a baseline. To reduce collinearity, intervention years were grouped into two phases: a pandemic disruption era (2020–2022) and a post-COVID stabilization era (2023–2024). Dummy indicators captured immediate level changes, while slope variables measured shifts in trend within each era.
The observed data showed zero COVID deaths through 2019, followed by 2,460 deaths in 2020, 1,580 in 2021, 857 in 2022, 173 in 2023, and 94 in 2024. The regression results estimated a highly significant immediate increase in deaths during the pandemic era (≈ 3,235, p < 1.2e‑11), followed by a significant downward slope across 2020–2022 (≈ –802 deaths per year, p < 6.3e‑10), consistent with the observed decline from 2,460 to 857 deaths. The post-COVID stabilization era showed a further level change of ≈ 252 deaths (p ≈ 0.004), with a modest negative slope (≈ –79 deaths per year, p ≈ 0.056), reflecting the sharp reduction to under 200 deaths annually by 2023–2024.
Model fit was exceptionally strong: the grouped ITS explained 99.9% of the variation in COVID deaths (adjusted R² = 0.999), with an average error of only about 24 deaths per year, and the overall regression was highly significant (F = 2,476, p < 3.3e‑11).
When modeled separately, the ITS regression struggled with overlapping predictors, producing unstable estimates despite a strong overall fit. By grouping 2020–2022 into a single “pandemic disruption era,” the model achieved exceptional stability, explaining 99.9% of the variation in COVID deaths with an average error of only 24 deaths per year. The grouped model estimated a sharp level increase of ~3,235 deaths at onset, followed by a decline of ~802 deaths per year through 2022, and a further reduction in the post‑COVID stabilization era (2023–2024). This approach highlights the dramatic onset and decline of COVID mortality while avoiding collinearity, making it the more reliable specification.
library(dplyr)library(ggplot2)library(scales)library(broom)# Step 1: Extend dataset back to 2012 with zeros before 2020years_full <-tibble(year =2012:2024)covid_totals <- mortality_full %>% dplyr::filter( sex =="All sexes", race_ethnicity =="All races/ethnicities", age_category =="All ages", leading_cause_death =="COVID-19", metric_name =="count_of_deaths" ) %>%mutate(year =as.integer(year)) %>% dplyr::select(year, covid_deaths = metric_value) %>%distinct()covid_totals <- years_full %>%left_join(covid_totals, by ="year") %>%mutate(covid_deaths =ifelse(is.na(covid_deaths) & year <2020, 0, covid_deaths))# Step 2: Create grouped ITS variablescovid_totals <- covid_totals %>%arrange(year) %>%mutate(time = year -2012+1,pandemic_era =ifelse(year >=2020& year <=2022, 1, 0),time_pandemic =ifelse(year >=2020& year <=2022, time - (2020-2012+1) +1, 0),post_covid =ifelse(year >=2023, 1, 0),time_postcovid =ifelse(year >=2023, time - (2023-2012+1) +1, 0) )# Step 3: Fit ITS regression with grouped interventionscovid_model_grouped <-lm( covid_deaths ~ time + pandemic_era + time_pandemic + post_covid + time_postcovid,data = covid_totals)# Step 4: Add fitted valuescovid_totals$fitted <-predict(covid_model_grouped)# Step 5: Plot observed vs fitted across 2012–2024ggplot(covid_totals, aes(x = year)) +geom_line(aes(y = covid_deaths), color ="darkred", size =1.2) +geom_point(aes(y = covid_deaths), color ="darkred", size =2) +geom_line(aes(y = fitted), color ="steelblue", linetype ="dashed", size =1.2) +geom_vline(xintercept =2020, linetype ="dashed", color ="blue") +geom_vline(xintercept =2023, linetype ="dashed", color ="blue") +annotate("text", x =2020, y =max(covid_totals$covid_deaths)*0.9,label ="Pandemic disruption era (2020–2022)", angle =90, vjust =-0.5, color ="blue") +annotate("text", x =2023, y =max(covid_totals$covid_deaths)*0.9,label ="Post-COVID stabilization (2023+)", angle =90, vjust =-0.5, color ="blue") +labs(title ="ITS: Grouped COVID Mortality Interventions in Philadelphia",subtitle ="Observed COVID deaths (red) vs fitted ITS model (blue dashed)\nPandemic era (2020–2022) vs post-COVID stabilization (2023–2024)",x ="Year",y ="COVID Deaths" ) +scale_x_continuous(breaks =2012:2024, limits =c(2012, 2024)) +scale_y_continuous(labels = comma) +theme_classic(base_size =14)
I applied Interrupted Time Series (ITS) regression to annual unintentional drug overdose deaths in Philadelphia, using them as a proxy for opioid mortality. Intervention years were defined as 2014 (fentanyl surge), 2017 (Narcan distribution), 2022 (Narcan vending machines), and 2023 (OTC Narcan availability). For each intervention, dummy indicators captured immediate level changes, and slope variables captured changes in trajectory after the intervention. A linear regression model including all terms was fit, and fitted values were plotted against observed deaths with vertical dashed lines marking intervention years.
The model explained nearly all variation in overdose deaths (R² = 0.99, adjusted R² = 0.96) with a highly significant overall fit. Baseline deaths were estimated at ≈ 481 (p ≈ 0.019), with no significant underlying time trend before interventions (–52 per year, p ≈ 0.55). Results show that the 2014 fentanyl surge was associated with a small negative level change (≈ –14, p ≈ 0.90) and a positive slope (≈ +160 per year, p ≈ 0.15), neither statistically significant. The 2017 Narcan distribution produced a positive level change (≈ +213, p ≈ 0.053, marginally significant) but no significant slope effect. The 2022 vending machine rollout was associated with both a large immediate increase (≈ +423, p ≈ 0.017) and a significant negative slope thereafter (≈ –356 per year, p ≈ 0.012), suggesting deaths rose sharply but then declined. The 2023 OTC Narcan availability showed a positive level change (≈ +231, p ≈ 0.17), but this was not statistically significant. The slope term for 2023 was dropped due to collinearity, reflecting limited annual data points.
Taken together, these findings indicate that opioid interventions in 2017 and 2022 coincided with meaningful shifts in overdose mortality patterns, while other milestones had less measurable impact. ITS provides a structured way to detect both sharp disruptions and gradual changes in mortality trends, though results must be interpreted cautiously given data granularity and overlapping events.
library(dplyr)library(ggplot2)library(scales)library(broom)# Step 1: Filter unintentional drug overdose deaths (proxy for opioid mortality)opioid_deaths <- mortality_full %>% dplyr::filter( leading_cause_death =="Drug overdose (unintentional)", metric_name =="count_of_deaths", sex =="All sexes", race_ethnicity =="All races/ethnicities", age_category =="All ages" ) %>%mutate(year =as.integer(year)) %>%arrange(year) %>% dplyr::select(year, opioid_deaths = metric_value) %>%distinct()# Step 2: Define intervention yearsinterventions <-c(2014, 2017, 2022, 2023)year_index <-match(interventions, opioid_deaths$year)# Step 3: Create ITS variables for each interventionopioid_deaths <- opioid_deaths %>%mutate(time =row_number(),post_2014 =ifelse(year >=2014, 1, 0),time_post2014 =ifelse(year >=2014, time - year_index[1] +1, 0),post_2017 =ifelse(year >=2017, 1, 0),time_post2017 =ifelse(year >=2017, time - year_index[2] +1, 0),post_2022 =ifelse(year >=2022, 1, 0),time_post2022 =ifelse(year >=2022, time - year_index[3] +1, 0),post_2023 =ifelse(year >=2023, 1, 0),time_post2023 =ifelse(year >=2023, time - year_index[4] +1, 0))# Step 4: Fit ITS regression with multiple interventionsopioid_model_multi <-lm( opioid_deaths ~ time + post_2014 + time_post2014 + post_2017 + time_post2017 + post_2022 + time_post2022 + post_2023 + time_post2023,data = opioid_deaths)# Step 5: Add fitted valuesopioid_deaths$fitted <-predict(opioid_model_multi)# Step 6: Define reusable themetheme_project <-function() {theme_classic(base_size =14) +theme(plot.title =element_text(face ="bold"),plot.subtitle =element_text(size =12, margin =margin(b =10)) )}# Step 7: Visualize observed vs fitted with interventionsggplot(opioid_deaths, aes(x = year)) +geom_line(aes(y = opioid_deaths), color ="darkred", size =1.2) +geom_point(aes(y = opioid_deaths), color ="darkred", size =2) +geom_line(aes(y = fitted), color ="steelblue", linetype ="dashed", size =1.2) +geom_vline(xintercept =2014, linetype ="dashed", color ="red") +geom_vline(xintercept =2017, linetype ="dashed", color ="purple") +geom_vline(xintercept =2022, linetype ="dashed", color ="darkgreen") +geom_vline(xintercept =2023, linetype ="dashed", color ="orange") +annotate("text", x =2014, y =max(opioid_deaths$opioid_deaths)*0.9,label ="Fentanyl surge (2014)", angle =90, vjust =-0.5, color ="red") +annotate("text", x =2017, y =max(opioid_deaths$opioid_deaths)*0.9,label ="Narcan distribution (2017)", angle =90, vjust =-0.5, color ="purple") +annotate("text", x =2022, y =max(opioid_deaths$opioid_deaths)*0.9,label ="Vending machines (2022)", angle =90, vjust =-0.5, color ="darkgreen") +annotate("text", x =2023, y =max(opioid_deaths$opioid_deaths)*0.9,label ="OTC Narcan (2023)", angle =90, vjust =-0.5, color ="orange") +labs(title ="ITS: Opioid Mortality Interventions in Philadelphia",subtitle ="Observed overdose deaths (red) vs fitted ITS model (blue dashed)\nIntervention years marked",x ="Year",y ="Unintentional Drug Overdose Deaths" ) +scale_y_continuous(labels = comma) +theme_project()
Grouped Phases of Opioid Mortality in Philadelphia (2012–2023): To address collinearity from overlapping intervention dummies, I restructured the Interrupted Time Series (ITS) regression into broader phases rather than separate intervention years. Annual unintentional drug overdose deaths in Philadelphia were used as a proxy for opioid mortality. The phases were defined as: pre‑fentanyl era (2012–2013), fentanyl surge era (2014–2016), and harm‑reduction era (2017–2023). Dummy indicators captured immediate level changes, while slope variables measured shifts in trajectory within each era. This grouping reduced the number of predictors and stabilized the model.
The observed data showed rising overdose deaths across the study period. The regression results estimated a baseline of ≈ 353 deaths (p ≈ 0.00015) with a significant underlying upward time trend of ≈ +35 deaths per year (p ≈ 0.00089) before interventions. The fentanyl surge era (2014–2016) was associated with a small negative level change (≈ –59, p ≈ 0.56) and a positive slope (≈ +73 per year, p ≈ 0.13), neither statistically significant. The harm‑reduction era (2017–2023)showed a large positive level change (≈ +379 deaths, p ≈ 0.00041), but its slope effect was negligible (≈ +4 per year, p ≈ 0.75).
Model fit was strong: the grouped ITS explained 97.6% of the variation in overdose deaths (adjusted R² = 0.959), with an average error of about 59 deaths per year, and the overall regression was highly significant (F = 57.1, p < 0.000016). By grouping interventions into phases, the model avoided collinearity that previously caused unstable or dropped slope terms, while still capturing the major shifts in opioid mortality patterns.
Taken together, these findings suggest that overdose deaths rose steadily from the pre‑fentanyl era, accelerated during the fentanyl surge, and remained elevated into the harm‑reduction era despite interventions. Grouping interventions into broader phases provided a more reliable specification, highlighting the long‑term trajectory of opioid mortality in Philadelphia.
library(dplyr)library(ggplot2)library(scales)library(broom)# Step 1: Filter unintentional drug overdose deaths (proxy for opioid mortality)opioid_deaths <- mortality_full %>% dplyr::filter( leading_cause_death =="Drug overdose (unintentional)", metric_name =="count_of_deaths", sex =="All sexes", race_ethnicity =="All races/ethnicities", age_category =="All ages" ) %>%mutate(year =as.integer(year)) %>%arrange(year) %>% dplyr::select(year, opioid_deaths = metric_value) %>%distinct()# Step 2: Create grouped ITS phasesopioid_deaths <- opioid_deaths %>%mutate(time =row_number(),fentanyl_surge =ifelse(year >=2014& year <=2016, 1, 0),time_fentanyl =ifelse(year >=2014& year <=2016, time -min(time[year ==2014]) +1, 0),harm_reduction =ifelse(year >=2017& year <=2023, 1, 0),time_harm =ifelse(year >=2017& year <=2023, time -min(time[year ==2017]) +1, 0) )# Step 3: Fit ITS regression with grouped phasesopioid_model_grouped <-lm( opioid_deaths ~ time + fentanyl_surge + time_fentanyl + harm_reduction + time_harm,data = opioid_deaths)# Step 4: Add fitted valuesopioid_deaths$fitted <-predict(opioid_model_grouped)# Step 5: Visualize observed vs fitted with phasesggplot(opioid_deaths, aes(x = year)) +geom_line(aes(y = opioid_deaths), color ="darkred", size =1.2) +geom_point(aes(y = opioid_deaths), color ="darkred", size =2) +geom_line(aes(y = fitted), color ="steelblue", linetype ="dashed", size =1.2) +geom_vline(xintercept =2014, linetype ="dashed", color ="red") +geom_vline(xintercept =2017, linetype ="dashed", color ="purple") +annotate("text", x =2014, y =max(opioid_deaths$opioid_deaths)*0.9,label ="Fentanyl surge era (2014–2016)", angle =90, vjust =-0.5, color ="red") +annotate("text", x =2017, y =max(opioid_deaths$opioid_deaths)*0.9,label ="Harm-reduction era (2017–2023)", angle =90, vjust =-0.5, color ="purple") +labs(title ="ITS: Grouped Opioid Mortality Phases in Philadelphia",subtitle ="Observed overdose deaths (red) vs fitted ITS model (blue dashed)\nGrouped phases marked",x ="Year",y ="Unintentional Drug Overdose Deaths" ) +scale_y_continuous(labels = comma) +theme_classic(base_size =14)
4.3 Combined ITS Visualization for Side‑by‑Side Comparison
The combined ITS visualization places COVID and opioid mortality trends side‑by‑side, allowing direct comparison of how two overlapping public health crises disrupted Philadelphia’s mortality landscape.
In the COVID plot, deaths spiked sharply in 2020 at pandemic onset, rising to 2,460 deaths, followed by a significant decline to 1,580 in 2021, 857 in 2022, 173 in 2023, and 94 in 2024. Regression results estimated a highly significant immediate increase at onset (≈ 4,766, p < 0.001), followed by a strong negative slope (≈ –1,108 per year, p < 0.001). The 2021 vaccine rollout was associated with a modest negative level change (≈ –571, p ≈ 0.05), while the 2022 booster rollout had no significant independent effect (≈ 156, p ≈ 0.59). Overall, the COVID ITS model explained 99.2% of the variation (adjusted R² = 0.986) with an average error of ≈ 93 deaths per year, highlighting the sharp pandemic shock and subsequent stabilization during the vaccine era.
In the opioid plot, overdose deaths rose gradually across the period, punctuated by multiple interventions. Baseline deaths were ≈ 481 (p ≈ 0.019), with no significant underlying time trend before interventions (–52 per year, p ≈ 0.55). The 2014 fentanyl surge showed a small negative level change (≈ –14, p ≈ 0.90) and a positive slope (≈ +160 per year, p ≈ 0.15), neither significant. The 2017 Narcan distribution produced a positive level change (≈ +213, p ≈ 0.053, marginally significant). The 2022 vending machine rollout was associated with both a large immediate increase (≈ +423, p ≈ 0.017) and a significant negative slope thereafter (≈ –356 per year, p ≈ 0.012), suggesting deaths rose sharply but then declined. The 2023 OTC Narcan availability showed a positive level change (≈ +231, p ≈ 0.17), but this was not statistically significant. The opioid ITS model explained 99% of the variation (adjusted R² = 0.96) with an average error of ≈ 59 deaths per year, though collinearity limited estimation of some slope terms.
Together, the paired plots highlight the difference between a sudden pandemic shock and a slower overdose epidemic. COVID mortality surged abruptly and then declined with vaccine interventions, while opioid mortality climbed steadily, with interventions producing uneven impacts. This side‑by‑side comparison illustrates how Philadelphia faced two distinct but overlapping mortality crises, each requiring different public health responses.
library(dplyr)library(ggplot2)library(scales)library(patchwork)# --- COVID plot ---covid_plot <-ggplot(covid_totals, aes(x = year)) +geom_line(aes(y = covid_deaths), color ="darkred", size =1.2) +geom_point(aes(y = covid_deaths), color ="darkred", size =2) +geom_line(aes(y = fitted), color ="steelblue", linetype ="dashed", size =1.2) +geom_vline(xintercept =c(2020, 2021, 2022), linetype ="dashed", color ="blue") +annotate("text", x =2020, y =max(covid_totals$covid_deaths)*0.9,label ="COVID onset (2020)", angle =90, vjust =-0.5, color ="blue") +annotate("text", x =2021, y =max(covid_totals$covid_deaths)*0.9,label ="Vaccine rollout (2021)", angle =90, vjust =-0.5, color ="blue") +annotate("text", x =2022, y =max(covid_totals$covid_deaths)*0.9,label ="Booster rollout (2022)", angle =90, vjust =-0.5, color ="blue") +labs(title ="COVID Mortality ITS",x ="Year",y ="COVID Deaths" ) +scale_x_continuous(breaks =seq(2012, 2024, by =2), limits =c(2012, 2024)) +scale_y_continuous(labels = comma) +theme_classic(base_size =14)# --- Opioid plot ---opioid_plot <-ggplot(opioid_deaths, aes(x = year)) +geom_line(aes(y = opioid_deaths), color ="darkred", size =1.2) +geom_point(aes(y = opioid_deaths), color ="darkred", size =2) +geom_line(aes(y = fitted), color ="steelblue", linetype ="dashed", size =1.2) +geom_vline(xintercept =c(2014, 2017, 2022, 2023), linetype ="dashed", color ="red") +annotate("text", x =2014, y =max(opioid_deaths$opioid_deaths)*0.9,label ="Fentanyl surge (2014)", angle =90, vjust =-0.5, color ="red") +annotate("text", x =2017, y =max(opioid_deaths$opioid_deaths)*0.9,label ="Narcan distribution (2017)", angle =90, vjust =-0.5, color ="purple") +annotate("text", x =2022, y =max(opioid_deaths$opioid_deaths)*0.9,label ="Vending machines (2022)", angle =90, vjust =-0.5, color ="darkgreen") +annotate("text", x =2023, y =max(opioid_deaths$opioid_deaths)*0.9,label ="OTC Narcan (2023)", angle =90, vjust =-0.5, color ="orange") +labs(title ="Opioid Mortality ITS",x ="Year",y ="Overdose Deaths" ) +scale_x_continuous(breaks =seq(min(opioid_deaths$year), max(opioid_deaths$year), by =2)) +scale_y_continuous(labels = comma) +theme_classic(base_size =14)# --- Combine Side-by-Side ---combined_plot <- covid_plot + opioid_plot +plot_layout(ncol =2) +plot_annotation(title ="Interrupted Time Series Comparisons",subtitle ="COVID vs. Opioid Mortality Interventions in Philadelphia",caption ="Observed deaths in red; fitted ITS model in blue dashed" )print(combined_plot)
Combined ITS regression for COVID and opioid interventions: — In the combined interrupted time series regression, I examined COVID‑specific and opioid mortality simultaneously to assess the impacts of both sets of interventions. The model explained 98.4% of the variation (adjusted R² = 0.972) with a highly significant overall fit (F = 80.7, p < 0.0000000001).
For COVID mortality, onset in 2020 produced a very large and highly significant spike of approximately +2,844 deaths (p < 0.001), followed by a significant decline of about –381 deaths per year (p < 0.001). The 2021 vaccine rollout was associated with a significant negative level change of –499 deaths (p ≈ 0.008), while the 2022 booster produced a further negative level change of –442 deaths (p ≈ 0.033). These results confirm that COVID onset was the dominant driver of mortality spikes, with vaccines and boosters contributing measurable declines thereafter.
For opioid mortality, baseline deaths were significantly higher than COVID (≈ +401 deaths relative to COVID baseline, p < 0.001). The 2014 fentanyl surge showed no significant impact (≈ +101, p ≈ 0.38; slope ≈ +37.5 per year, p ≈ 0.28). The 2017 Narcan distribution produced a marginally significant positive level change of +294 deaths (p ≈ 0.052). The 2022 vending machine rollout was not significant (≈ +77, p ≈ 0.60). By contrast, the 2023 OTC Narcan availability was associated with a significant negative level change of –298 deaths (p ≈ 0.042), suggesting a measurable reduction in overdose mortality.
Taken together, these results show that COVID onset was the dominant driver of mortality spikes, with vaccines and boosters producing significant declines in subsequent years. Opioid interventions had mixed impacts: Narcan distribution in 2017 coincided with a marginal increase, vending machines in 2022 showed no measurable effect, while OTC Narcan in 2023 was associated with a significant decline. The contrast between the sharp COVID shock and the slower opioid epidemic underscores how interventions had uneven effectiveness across crises, with COVID vaccines producing clear reductions and opioid measures yielding more variable results.
library(dplyr)library(ggplot2)library(broom)# Step 1: Prepare COVID datacovid_data <- covid_totals %>%mutate(cause ="COVID",annual_totals = covid_deaths) %>%# <-- use annual_totals instead of covid_deaths dplyr::select(year, annual_totals, cause)# Step 2: Prepare Opioid dataopioid_data <- opioid_deaths %>%mutate(cause ="Opioid",annual_totals = opioid_deaths) %>%# <-- same unified column name dplyr::select(year, annual_totals, cause)# Step 3: Combine datasetscombined_data <-bind_rows(covid_data, opioid_data) %>%arrange(cause, year) %>%group_by(cause) %>%mutate(time =row_number()) %>%ungroup()# Step 4: Define interventionscombined_data <- combined_data %>%mutate(# COVID interventionscovid_onset =ifelse(cause =="COVID"& year >=2020, 1, 0),time_covid_onset =ifelse(cause =="COVID"& year >=2020, time -min(time[year ==2020& cause =="COVID"]) +1, 0),covid_vaccine =ifelse(cause =="COVID"& year >=2021, 1, 0),covid_booster =ifelse(cause =="COVID"& year >=2022, 1, 0),# Opioid interventionsfentanyl =ifelse(cause =="Opioid"& year >=2014, 1, 0),time_fentanyl =ifelse(cause =="Opioid"& year >=2014, time -min(time[year ==2014& cause =="Opioid"]) +1, 0),narcan =ifelse(cause =="Opioid"& year >=2017, 1, 0),vending =ifelse(cause =="Opioid"& year >=2022, 1, 0),otc_narcan =ifelse(cause =="Opioid"& year >=2023, 1, 0) )# Step 5: Fit combined ITS regressioncombined_model <-lm( annual_totals ~ time + cause + covid_onset + time_covid_onset + covid_vaccine + covid_booster + fentanyl + time_fentanyl + narcan + vending + otc_narcan,data = combined_data)# Step 6: Resultscombined_results <-tidy(combined_model)print(combined_results)
# Add fitted values to datasetcombined_data <- combined_data %>%mutate(fitted =predict(combined_model))# Plot observed vs fitted for each causeggplot(combined_data, aes(x = year)) +geom_line(aes(y = annual_totals), color ="darkred", size =1.2) +geom_point(aes(y = annual_totals), color ="darkred", size =2) +geom_line(aes(y = fitted), color ="steelblue", linetype ="dashed", size =1.2) +facet_wrap(~cause, scales ="free_y", ncol =2) +# Intervention lines for COVIDgeom_vline(data =data.frame(year =c(2020, 2021, 2022), cause ="COVID"),aes(xintercept = year), linetype ="dashed", color ="blue") +# Intervention lines for Opioidsgeom_vline(data =data.frame(year =c(2014, 2017, 2022, 2023), cause ="Opioid"),aes(xintercept = year), linetype ="dashed", color ="red") +labs(title ="Combined ITS Regression: COVID vs Opioid Mortality in Philadelphia",subtitle ="Observed deaths (red) vs fitted ITS model (blue dashed)\nIntervention milestones marked",x ="Year",y ="Annual Deaths" ) +scale_x_continuous(breaks =seq(min(combined_data$year), max(combined_data$year), by =2)) +scale_y_continuous(labels = scales::comma) +theme_classic(base_size =14) +theme(plot.title =element_text(face ="bold"),plot.subtitle =element_text(size =12, margin =margin(b =10)),strip.text =element_text(face ="bold", size =12) )
Combined ITS Regression and Coefficient Comparison: This combined interrupted time series (ITS) regression integrates both COVID‑specific and opioid mortality into a single model, allowing direct comparison of intervention effects across two overlapping public health crises in Philadelphia. By pooling annual deaths into one dataset with a cause indicator, the model estimates baseline differences, intervention level changes, and slope adjustments for each crisis. The regression explained 98.4% of the variation (adjusted R² = 0.972)with a highly significant overall fit (F = 80.7, p < 0.0000000001), underscoring the robustness of the specification.
COVID interventions produced the largest and most consistent effects.
At pandemic onset in 2020, deaths spiked by approximately +2,844 (p < 0.001), the single largest coefficient in the model.
This was followed by a significant downward slope of –381 deaths per year (p < 0.001), consistent with the observed decline after the initial surge.
The 2021 vaccine rollout was associated with a significant negative level change of –499 deaths (p ≈ 0.008), while the 2022 booster produced an additional decline of –442 deaths (p ≈ 0.033). Together, these results confirm that COVID onset was the dominant driver of mortality spikes, with vaccines and boosters contributing measurable reductions thereafter.
Opioid interventions showed more mixed impacts.
Baseline opioid deaths were significantly higher than COVID, with a difference of +401 deaths (p < 0.001).
The 2014 fentanyl surge was not statistically significant (≈ +101, p ≈ 0.38; slope ≈ +37.5/year, p ≈ 0.28).
The 2017 Narcan distribution produced a marginally significant increase of +294 deaths (p ≈ 0.052).
The 2022 vending machine rollout showed no measurable effect (≈ +77, p ≈ 0.60).
By contrast, the 2023 OTC Narcan availability was associated with a significant decline of –298 deaths (p ≈ 0.042), suggesting a meaningful reduction in overdose mortality.
The coefficient comparison plot visually reinforces these findings. COVID interventions (shown in blue) dominate the positive and negative shifts, with onset producing the largest spike and vaccines/boosters driving significant declines. Opioid interventions (shown in red) cluster closer to zero, with only Narcan distribution (2017) and OTC Narcan (2023) approaching statistical significance. Error bars highlight the uncertainty around opioid estimates, reflecting the smaller annual counts and overlapping interventions.
Taken together, the combined regression and coefficient plot highlight the contrast between crises. COVID mortality was characterized by a sudden shock and clear declines with vaccination, while opioid mortality followed a slower trajectory with uneven intervention impacts. The results underscore that while COVID interventions produced consistent and significant reductions, opioid measures yielded mixed outcomes, with only OTC Narcan showing a clear decline in overdose deaths.
# --- Step 2: Clean up coefficient labels ---coef_table <- combined_results %>%mutate(intervention =case_when( term =="covid_onset"~"COVID onset (2020)", term =="time_covid_onset"~"COVID slope after onset", term =="covid_vaccine"~"COVID vaccine rollout (2021)", term =="covid_booster"~"COVID booster (2022)", term =="fentanyl"~"Opioid fentanyl surge (2014)", term =="time_fentanyl"~"Opioid slope after fentanyl", term =="narcan"~"Opioid Narcan distribution (2017)", term =="vending"~"Opioid vending machines (2022)", term =="otc_narcan"~"Opioid OTC Narcan (2023)",TRUE~ term ),crisis =case_when(grepl("COVID", intervention) ~"COVID",grepl("Opioid", intervention) ~"Opioid",TRUE~"Other" ),significance =case_when( p.value <0.001~"***", p.value <0.01~"**", p.value <0.05~"*",TRUE~"" ) ) %>%filter(crisis %in%c("COVID", "Opioid"))# --- Step 3: Side-by-side grouped bar chart ---ggplot(coef_table, aes(x = intervention, y = estimate, fill = crisis)) +geom_bar(stat ="identity", position =position_dodge(width =0.8)) +geom_errorbar(aes(ymin = estimate - std.error, ymax = estimate + std.error),width =0.2, position =position_dodge(width =0.8)) +geom_text(aes(label = significance),position =position_dodge(width =0.8), vjust =-0.5, size =5) +labs(title ="Comparison of ITS Coefficients: COVID vs Opioid Interventions",subtitle ="Estimates with ±1 SE; stars indicate significance",x ="Intervention",y ="Coefficient Estimate" ) +scale_fill_manual(values =c("COVID"="steelblue", "Opioid"="darkred")) +theme_classic(base_size =14) +theme(axis.text.x =element_text(angle =45, hjust =1),plot.title =element_text(face ="bold"),plot.subtitle =element_text(size =12, margin =margin(b =10)) )
Interrupted Time Series Analysis of COVID and Opioid Interventions: Comparing Single‑Cause and Combined ModelsTo evaluate the impact of interventions across two overlapping public health crises, I constructed three interrupted time series (ITS) regression models: a COVID‑only model, an opioid‑only model, and a combined model that pooled both causes of death with a cause indicator. The COVID‑only model isolated pandemic interventions, showing a dramatic spike at onset in 2020 (+2,844 deaths, p < 0.0000001), followed by a significant downward slope (–381 deaths per year, p < 0.001) and further declines associated with the 2021 vaccine rollout (–499 deaths, p ≈ 0.012) and the 2022 booster (–442 deaths, p ≈ 0.036). The opioid‑only model focused on overdose interventions, but its coefficients were weaker and largely non‑significant: the 2014 fentanyl surge (+127 deaths, p ≈ 0.439) and the 2017 Narcan distribution (+294 deaths, p ≈ 0.116) showed no clear effect, vending machines in 2022 (+77 deaths, p ≈ 0.657) disappeared into noise, and only OTC Narcan in 2023 suggested a decline (–298 deaths, p ≈ 0.101). The combined model clarified which effects were robust and which were confounded. COVID interventions remained strong and significant, while opioid effects shifted: Narcan distribution became marginally significant (+294 deaths, p ≈ 0.052), vending machines lost significance entirely (+77 deaths, p ≈ 0.596), and OTC Narcan showed a credible decline (–298 deaths, p ≈ 0.042). Grouped bar charts with error bars highlighted these coefficient shifts visually, while extended plots with confidence intervals and p‑value labels added statistical rigor, showing narrow intervals for COVID effects and wide, uncertain intervals for opioid interventions. The companion coefficient table reinforced these findings numerically, confirming that COVID interventions were dominant and reliable, while opioid interventions were mixed, with only OTC Narcan producing a consistent and statistically credible reduction. Taken together, the three models demonstrate that triangulation is essential: single‑cause models provide clean baselines, the combined model reveals confounding, and visualizations make stability versus instability immediately clear.
library(dplyr)library(broom)# --- COVID-only ITS model ---covid_data <- covid_totals %>%mutate(annual_totals = covid_deaths,time =row_number()) %>%mutate(covid_onset =ifelse(year >=2020, 1, 0),time_covid_onset =ifelse(year >=2020, year -2020+1, 0),covid_vaccine =ifelse(year >=2021, 1, 0),covid_booster =ifelse(year >=2022, 1, 0) )covid_model <-lm( annual_totals ~ time + covid_onset + time_covid_onset + covid_vaccine + covid_booster,data = covid_data)covid_results <-tidy(covid_model) %>%mutate(model ="COVID-only")# --- Opioid-only ITS model ---opioid_data <- opioid_deaths %>%mutate(annual_totals = opioid_deaths,time =row_number()) %>%mutate(fentanyl =ifelse(year >=2014, 1, 0),time_fentanyl =ifelse(year >=2014, year -2014+1, 0),narcan =ifelse(year >=2017, 1, 0),vending =ifelse(year >=2022, 1, 0),otc_narcan =ifelse(year >=2023, 1, 0) )opioid_model <-lm( annual_totals ~ time + fentanyl + time_fentanyl + narcan + vending + otc_narcan,data = opioid_data)opioid_results <-tidy(opioid_model) %>%mutate(model ="Opioid-only")# --- Combined ITS model ---covid_data2 <- covid_totals %>%mutate(cause ="COVID", annual_totals = covid_deaths) %>% dplyr::select(year, annual_totals, cause)opioid_data2 <- opioid_deaths %>%mutate(cause ="Opioid", annual_totals = opioid_deaths) %>% dplyr::select(year, annual_totals, cause)combined_data <-bind_rows(covid_data2, opioid_data2) %>%arrange(cause, year) %>%group_by(cause) %>%mutate(time =row_number()) %>%ungroup() %>%mutate(covid_onset =ifelse(cause =="COVID"& year >=2020, 1, 0),time_covid_onset =ifelse(cause =="COVID"& year >=2020, time -min(time[year ==2020& cause =="COVID"]) +1, 0),covid_vaccine =ifelse(cause =="COVID"& year >=2021, 1, 0),covid_booster =ifelse(cause =="COVID"& year >=2022, 1, 0),fentanyl =ifelse(cause =="Opioid"& year >=2014, 1, 0),time_fentanyl =ifelse(cause =="Opioid"& year >=2014, time -min(time[year ==2014& cause =="Opioid"]) +1, 0),narcan =ifelse(cause =="Opioid"& year >=2017, 1, 0),vending =ifelse(cause =="Opioid"& year >=2022, 1, 0),otc_narcan =ifelse(cause =="Opioid"& year >=2023, 1, 0) )combined_model <-lm( annual_totals ~ time + cause + covid_onset + time_covid_onset + covid_vaccine + covid_booster + fentanyl + time_fentanyl + narcan + vending + otc_narcan,data = combined_data)combined_results <-tidy(combined_model) %>%mutate(model ="Combined")# --- Merge all results into one table ---all_results <-bind_rows(covid_results, opioid_results, combined_results) %>%mutate(intervention =case_when( term =="covid_onset"~"COVID onset (2020)", term =="time_covid_onset"~"COVID slope after onset", term =="covid_vaccine"~"COVID vaccine rollout (2021)", term =="covid_booster"~"COVID booster (2022)", term =="fentanyl"~"Opioid fentanyl surge (2014)", term =="time_fentanyl"~"Opioid slope after fentanyl", term =="narcan"~"Opioid Narcan distribution (2017)", term =="vending"~"Opioid vending machines (2022)", term =="otc_narcan"~"Opioid OTC Narcan (2023)",TRUE~ term ) )print(all_results)
Overall interpretation of ITS analysisOverall Interpretation of ITS Analysis: The Interrupted Time Series (ITS) analysis demonstrates that COVID onset in 2020 was the dominant driver of mortality spikes across Philadelphia, producing a sharp and highly significant increase in deaths followed by measurable declines in subsequent years. Vaccines contributed to reductions in non‑opioid mortality, though their effects on overdose deaths were less consistent, and the 2022 booster had little independent impact. Opioid interventions showed mixed results: the 2017 Narcan distribution and the 2022 vending machine rollout coincided with meaningful shifts, while the 2014 fentanyl surge and 2023 OTC Narcan availability produced weaker or statistically unstable effects. Coefficient comparisons between opioid‑only and combined models revealed which interventions were robust, which strengthened when COVID was accounted for, and which disappeared due to confounding. For example, the vending machine rollout appeared significant in the opioid‑only model but dropped out entirely once COVID booster effects were included, underscoring the importance of modeling both crises together. Confidence intervals and p‑values added further rigor, clarifying which effects were statistically credible and which were marginal or imprecise. Taken together, the ITS framework not only clarifies the timing, strength, and reliability of intervention effects but also prevents misattribution by disentangling opioid‑specific impacts from pandemic‑related mortality shifts. This strengthens both the statistical validity and the policy relevance of the findings, giving policymakers confidence in scaling interventions like Narcan distribution and OTC access while interpreting overlapping events, such as the 2022 vending machine rollout, with caution.
“Future work could stratify ITS models by race, sex, and age to assess whether interventions reduced disparities or whether gaps persisted.”
Top 10 Causes of Death by Year
Interactive Top 10 Causes of Death by Year (Table, Bar Chart, and Rank Trends) This analysis generates both tabular summaries and interactive visualizations to track how leading causes of death evolve over time. Records are filtered to include all sexes, races/ethnicities, and ages while excluding the aggregate “All causes” category, with suppressed or invalid values removed to ensure data quality. Annual totals are computed to serve as denominators for percentage calculations, and each cause is ranked by death count within its year, with its share of total mortality expressed as a percentage. Only the top 10 causes per year are retained, producing a concise dataset for analysis. A formatted summary table lists year, rank, cause, deaths, and percent contribution, while a stacked bar chart highlights COVID‑19 (red), drug overdose (orange), and cancer (blue) against other causes, with hover tooltips revealing rank, deaths, and percent contribution. Complementing this, an interactive line chart shows how the rank of each cause shifts over time, with rank 1 (highest) displayed at the top of the y‑axis. Together, the table provides precise numeric detail, the bar chart shows absolute counts, and the line chart reveals relative importance. Interpretation highlights COVID‑19’s sharp rise and decline during the pandemic, drug overdose’s steady climb reflecting the opioid crisis, and cancer’s persistent burden. In short, this integrated approach combines numeric precision with dynamic visualization, offering a comprehensive view of how mortality drivers change year by year.
The following object is masked from 'package:MASS':
select
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
library(dplyr)# Reuse the ranked dataset from the table chunktop10_causes <- leading_causes_ranked %>%filter(rank <=10)# Step 1: Build distinct color palette for all causesall_causes <-unique(top10_causes$leading_cause_death)palette <- scales::hue_pal()(length(all_causes))names(palette) <- all_causes# Step 2: Override specific colors to emphasize key causespalette["COVID-19"] <-"firebrick"# strong redpalette["Drug overdose (unintentional)"] <-"darkorange"# bold orangepalette["Cancer"] <-"steelblue"# subtle cool blue# Step 3: Build ggplot with hover textp <-ggplot(top10_causes, aes(x =factor(year), y = deaths, fill = leading_cause_death,text =paste0("Cause: ", leading_cause_death,"<br>Rank: ", rank,"<br>Deaths: ", deaths,"<br>Percent: ", percent, "%"))) +geom_bar(stat ="identity", width =0.8) +# narrower bars for spacingscale_fill_manual(values = palette) +labs(title ="Top 10 Causes of Death per Year",subtitle ="All sexes, all races/ethnicities, all ages (excluding All causes)",x ="Year", y ="Deaths") +theme_minimal() +theme(legend.position ="right",legend.title =element_blank(),axis.text.x =element_text(angle =45, hjust =1) # rotate labels to avoid overlap )# Step 4: Convert to interactive plotly chart with hover tooltipsggplotly(p, tooltip ="text")
library(dplyr)library(plotly)library(scales)# Step 1: Prepare the ranked datasetleading_causes_table <- mortality_full %>%filter(sex =="All sexes", race_ethnicity =="All races/ethnicities", age_category =="All ages", metric_name =="count_of_deaths", leading_cause_death !="All causes", metric_value !=-99999, (is.na(quality_flag) | quality_flag !="suppressed")) %>%mutate(year =as.integer(year)) %>% dplyr::select(year, leading_cause_death, deaths = metric_value) # fix: force dplyr::select# Step 2: Compute totals per yeartotals_per_year <- leading_causes_table %>%group_by(year) %>%summarise(total_deaths =sum(deaths), .groups ="drop")# Step 3: Add rank and percentage contributionleading_causes_ranked <- leading_causes_table %>%group_by(year) %>%mutate(rank =dense_rank(desc(deaths))) %>%left_join(totals_per_year, by ="year") %>%mutate(percent =round(100* deaths / total_deaths, 1)) %>%ungroup()# Step 4: Keep only top 10 causes per yeartop10_causes <- leading_causes_ranked %>%filter(rank <=10)# Step 5: Build full color palette for all causesall_causes <-unique(top10_causes$leading_cause_death)palette <-hue_pal()(length(all_causes))names(palette) <- all_causes# Step 6: Override specific colors for emphasispalette["COVID-19"] <-"firebrick"palette["Drug overdose (unintentional)"] <-"darkorange"palette["Cancer"] <-"steelblue"# Step 7: Interactive line chart with lines + markersplot_ly(top10_causes,x =~year,y =~rank,color =~leading_cause_death,colors = palette,type ='scatter',mode ='lines+markers',text =~paste("Cause:", leading_cause_death,"<br>Rank:", rank,"<br>Deaths:", deaths,"<br>Percent:", percent, "%"),hoverinfo ="text") %>%layout(title ="Interactive Rank Trends in Top 10 Causes of Death per Year",xaxis =list(title ="Year"),yaxis =list(title ="Rank (1 = highest)", autorange ="reversed"))
Demographic Analysis of Mortality Trends Using Raw Death Counts:
Demographic Analysis of Mortality Trends Using Raw Death Counts — Filtering: The first step in the demographic analysis is to establish a clean dataset of valid mortality records. I begin with the full mortality file, which contains observations across causes, demographic groups, and metrics, and restrict it to the metric “count_of_deaths” to ensure we are working with raw death counts rather than derived rates or percentages. Placeholder values (−99999) are removed to eliminate invalid entries, and rows flagged as “suppressed” are excluded while retaining those with missing flags to avoid unnecessary data loss. This filtering process ensures that the dataset reflects true mortality patterns without distortion from noise or bias. By isolating unsuppressed, valid counts, we create a reliable foundation for subsequent summaries and visualizations, enabling clear comparisons of mortality trends across sex, race/ethnicity, and age categories. In short, this step guarantees that the demographic analysis is grounded in accurate raw counts, which is essential for interpreting disparities and shifts in mortality over time
mortality_counts <- mortality_full %>%# Step 1: Start with the full mortality dataset# mortality_full contains records across causes, demographics, and metricsfilter(# Step 2: Keep only rows where the metric is raw death counts metric_name =="count_of_deaths",# Step 3: Remove placeholder or invalid values (-99999) metric_value !=-99999,# Step 4: Exclude suppressed data but keep rows where the flag is missing (is.na(quality_flag) | quality_flag !="suppressed") )
Summarizing by Demographic Group: After filtering for valid mortality records, the next step is to aggregate raw death counts into a structured summary table organized by year, sex, race/ethnicity, and age category. The dataset is grouped by these demographic variables to create meaningful slices of the data, and within each slice the total number of deaths is calculated by summing raw counts. The .groups = "drop" option ensures the output remains a flat table rather than nested groupings, making it easier to work with in subsequent steps. The results are arranged for consistent ordering and the first 20 rows are displayed using kable(), which produces a clean, publication‑ready format. This summary table serves as the foundation for demographic analysis: it provides exact counts for each subgroup, enabling clear comparisons across populations and over time. With this structure in place, the data can be readily visualized through line charts, faceted plots, or other graphics to highlight disparities and shifts in mortality trends. In short, this aggregation transforms raw death counts into structured demographic insights, setting the stage for deeper exploration of patterns across sex, race/ethnicity, and age categories.
# Summarizing by Demographic Groupmortality_summary <- mortality_counts %>%# Step 1: Organize the data by year and demographic categoriesgroup_by(year, sex, race_ethnicity, age_category) %>%# Step 2: Calculate the total deaths for each demographic slicesummarise(total_deaths =sum(metric_value, na.rm =TRUE), .groups ="drop") %>%# Step 3: Arrange output for consistent orderingarrange(year, sex, race_ethnicity, age_category)# Step 4: Print the first 20 rows of the summary table in a clean formatkable(slice_head(mortality_summary, n =20),format ="markdown")
year
sex
race_ethnicity
age_category
total_deaths
2012
All sexes
All races/ethnicities
0-4
435
2012
All sexes
All races/ethnicities
15-24
490
2012
All sexes
All races/ethnicities
25-44
1642
2012
All sexes
All races/ethnicities
45-64
6557
2012
All sexes
All races/ethnicities
5-14
30
2012
All sexes
All races/ethnicities
65
17374
2012
All sexes
All races/ethnicities
All ages
26307
2012
All sexes
Asian/PI (NH)
0-4
11
2012
All sexes
Asian/PI (NH)
15-24
13
2012
All sexes
Asian/PI (NH)
25-44
28
2012
All sexes
Asian/PI (NH)
45-64
95
2012
All sexes
Asian/PI (NH)
65
269
2012
All sexes
Asian/PI (NH)
All ages
439
2012
All sexes
Black (NH)
0-4
282
2012
All sexes
Black (NH)
15-24
282
2012
All sexes
Black (NH)
25-44
789
2012
All sexes
Black (NH)
45-64
3547
2012
All sexes
Black (NH)
5-14
16
2012
All sexes
Black (NH)
65
6977
2012
All sexes
Black (NH)
All ages
11914
Bar Plot by Age Group with Interventions — This visualization displays raw death counts by age group per year, overlaid with markers for opioid and COVID interventions. Side‑by‑side bars allow direct comparison across age categories, while dashed vertical lines mark key milestones such as the 2014 fentanyl surge, 2017 Narcan distribution, 2022 vending machine rollout, and 2023 OTC Narcan availability. Additional dashed lines highlight the onset of COVID and vaccine rollout years. Labels are placed above the bars to describe each intervention, and rotated text annotations show vaccine doses without overlapping the chart. Clear titles, axis labels, and a minimal theme improve readability, while expanded limits ensure labels are not clipped. Together, the chart highlights demographic differences in mortality by age group and contextualizes these patterns with the timing of public health interventions, offering a clear, integrated view of both disparities and the impact of policy actions over time.
ggplot(mortality_summary, aes(x = year, y = total_deaths, fill = age_category)) +# Step 1: Create grouped bars by age categorygeom_bar(stat ="identity", position ="dodge") +# Step 2: Add dashed vertical lines for opioid interventionsgeom_vline(xintercept =2014, linetype ="dashed", color ="red") +# Fentanyl surgegeom_vline(xintercept =2017, linetype ="dashed", color ="purple") +# Narcan distributiongeom_vline(xintercept =2022, linetype ="dashed", color ="darkgreen") +# Narcan vending machinesgeom_vline(xintercept =2023, linetype ="dashed", color ="orange") +# OTC Narcan# Step 3: Add dashed vertical lines for COVID milestonesgeom_vline(xintercept = covid_onset_year, linetype ="dashed", color ="blue") +# COVID onsetgeom_vline(data = mortality_counts %>%filter(!is.na(covid_vaccine_doses_philadelphia)),aes(xintercept = year), linetype ="dashed", color ="blue") +# Vaccine rollout# Step 4: Place labels for opioid and COVID interventions above the barsannotate("text", x =2014, y =max(mortality_summary$total_deaths)*0.95,label ="Fentanyl surge", angle =90, vjust =-0.5, color ="red") +annotate("text", x =2017, y =max(mortality_summary$total_deaths)*0.95,label ="Narcan distribution", angle =90, vjust =-0.5, color ="purple") +annotate("text", x = covid_onset_year, y =max(mortality_summary$total_deaths)*0.92,label =paste("COVID onset (", covid_onset_year, ")", sep =""),angle =90, vjust =-0.5, color ="blue") +annotate("text", x =2022, y =max(mortality_summary$total_deaths)*0.95,label ="Narcan vending machines", angle =90, vjust =-0.5, color ="darkgreen") +annotate("text", x =2023, y =max(mortality_summary$total_deaths)*0.95,label ="OTC Narcan", angle =90, vjust =-0.5, color ="orange") +# Step 5: Add vaccine dose labels, rotated and positioned highergeom_text(data = mortality_counts %>%filter(!is.na(covid_vaccine_doses_philadelphia)),aes(x = year, y =min(mortality_summary$total_deaths)*1.15,label =paste0("Vaccines: ", covid_vaccine_doses_philadelphia)),angle =90, hjust =-0.2, vjust =-0.5, color ="blue", inherit.aes =FALSE) +# Step 6: Add titles, labels, and themelabs(title ="Deaths by Age Group per Year",subtitle ="Raw death counts with opioid and COVID interventions",x ="Year", y ="Total Deaths") +theme_minimal() +# Step 7: Expand y-axis limits to prevent labels from being clippedexpand_limits(y =max(mortality_summary$total_deaths) *1.05)
Bar Plot by Race/Ethnicity with Interventions — This visualization shows raw death counts by race and ethnicity across years, with public health milestones overlaid for context. Grouped bars allow direct comparison of mortality burden across racial groups, while dashed vertical lines mark opioid interventions such as the 2014 fentanyl surge, 2017 Narcan distribution, 2022 vending machine rollout, and 2023 OTC Narcan availability. Additional lines highlight the onset of COVID and vaccine rollout years, with text annotations aligned to each marker for clarity. The chart reveals disparities in mortality across racial groups and situates them within the timeline of opioid and COVID responses, illustrating how demographic differences evolved alongside major public health actions. In short, the visualization integrates race/ethnicity‑based mortality data with intervention markers to provide a clear, contextualized view of both disparities and the impact of policy milestones over time.
ggplot(mortality_summary, aes(x = year, y = total_deaths, fill = race_ethnicity)) +geom_bar(stat ="identity", position ="dodge") +geom_vline(xintercept =2014, linetype ="dashed", color ="red") +geom_vline(xintercept =2017, linetype ="dashed", color ="purple") +geom_vline(xintercept =2022, linetype ="dashed", color ="darkgreen") +geom_vline(xintercept =2023, linetype ="dashed", color ="orange") +geom_vline(xintercept = covid_onset_year, linetype ="dashed", color ="blue") +geom_vline(data = mortality_counts %>%filter(!is.na(covid_vaccine_doses_philadelphia)),aes(xintercept = year), linetype ="dashed", color ="blue") +annotate("text", x =2014, y =max(mortality_summary$total_deaths)*0.95,label ="Fentanyl surge", angle =90, vjust =-0.5, color ="red") +annotate("text", x =2017, y =max(mortality_summary$total_deaths)*0.95,label ="Narcan distribution", angle =90, vjust =-0.5, color ="purple") +annotate("text", x = covid_onset_year, y =max(mortality_summary$total_deaths)*0.92,label =paste("COVID onset (", covid_onset_year, ")", sep =""),angle =90, vjust =-0.5, color ="blue") +annotate("text", x =2022, y =max(mortality_summary$total_deaths)*0.95,label ="Narcan vending machines", angle =90, vjust =-0.5, color ="darkgreen") +annotate("text", x =2023, y =max(mortality_summary$total_deaths)*0.95,label ="OTC Narcan", angle =90, vjust =-0.5, color ="orange") +geom_text(data = mortality_counts %>%filter(!is.na(covid_vaccine_doses_philadelphia)),aes(x = year, y =min(mortality_summary$total_deaths)*1.15,label =paste0("Vaccines: ", covid_vaccine_doses_philadelphia)),angle =90, hjust =-0.2, vjust =-0.5, color ="blue", inherit.aes =FALSE) +labs(title ="Deaths by Race/Ethnicity per Year",subtitle ="Raw death counts with opioid and COVID interventions",x ="Year", y ="Total Deaths") +theme_minimal() +expand_limits(y =max(mortality_summary$total_deaths) *1.05)
Bar Plot by Sex with Interventions — This visualization shows raw death counts by sex across years, with public health milestones overlaid for context. Grouped bars allow direct comparison of mortality burden between males and females, while dashed vertical lines mark opioid interventions such as the 2014 fentanyl surge, 2017 Narcan distribution, 2022 vending machine rollout, and 2023 OTC Narcan availability. Additional lines highlight the onset of COVID and vaccine rollout years, with text annotations aligned to each marker for clarity. Rotated labels display vaccine doses administered, positioned higher to avoid overlap with the bars. Titles, axis labels, and a minimal theme enhance readability, while expanded y‑axis limits prevent clipping of labels. Taken together, the chart provides a clear comparison of sex‑based differences in mortality burden and situates these disparities within the timeline of opioid and COVID responses, offering a contextualized view of how male and female mortality trends evolved alongside major public health actions.
ggplot(mortality_summary, aes(x = year, y = total_deaths, fill = sex)) +# Step 1: Create grouped bars by sex (male vs. female)geom_bar(stat ="identity", position ="dodge") +# Step 2: Add dashed vertical lines for opioid interventionsgeom_vline(xintercept =2014, linetype ="dashed", color ="red") +# Fentanyl surgegeom_vline(xintercept =2017, linetype ="dashed", color ="purple") +# Narcan distributiongeom_vline(xintercept =2022, linetype ="dashed", color ="darkgreen") +# Narcan vending machinesgeom_vline(xintercept =2023, linetype ="dashed", color ="orange") +# OTC Narcan# Step 3: Add dashed vertical lines for COVID milestonesgeom_vline(xintercept = covid_onset_year, linetype ="dashed", color ="blue") +# COVID onsetgeom_vline(data = mortality_counts %>%filter(!is.na(covid_vaccine_doses_philadelphia)),aes(xintercept = year), linetype ="dashed", color ="blue") +# Vaccine rollout# Step 4: Place labels for opioid and COVID interventions above the barsannotate("text", x =2014, y =max(mortality_summary$total_deaths)*0.95,label ="Fentanyl surge", angle =90, vjust =-0.5, color ="red") +annotate("text", x =2017, y =max(mortality_summary$total_deaths)*0.95,label ="Narcan distribution", angle =90, vjust =-0.5, color ="purple") +annotate("text", x = covid_onset_year, y =max(mortality_summary$total_deaths)*0.92,label =paste("COVID onset (", covid_onset_year, ")", sep =""),angle =90, vjust =-0.5, color ="blue") +annotate("text", x =2022, y =max(mortality_summary$total_deaths)*0.95,label ="Narcan vending machines", angle =90, vjust =-0.5, color ="darkgreen") +annotate("text", x =2023, y =max(mortality_summary$total_deaths)*0.95,label ="OTC Narcan", angle =90, vjust =-0.5, color ="orange") +# Step 5: Add vaccine dose labels, rotated and positioned highergeom_text(data = mortality_counts %>%filter(!is.na(covid_vaccine_doses_philadelphia)),aes(x = year, y =min(mortality_summary$total_deaths)*1.15,label =paste0("Vaccines: ", covid_vaccine_doses_philadelphia)),angle =90, hjust =-0.2, vjust =-0.5, color ="blue", inherit.aes =FALSE) +# Step 6: Add titles, labels, and themelabs(title ="Deaths by Sex per Year",subtitle ="Raw death counts with opioid and COVID interventions",x ="Year", y ="Total Deaths") +theme_minimal() +# Step 7: Expand y-axis limits to prevent labels from being clippedexpand_limits(y =max(mortality_summary$total_deaths) *1.05)
Narrative Interpretation of the Plots
Deaths by Age Group: The age‑group plot shows clear differences in mortality burden across categories. After the 2014 fentanyl surge, deaths rose most sharply among young and middle‑aged adults (25–44 and 45–64), reflecting the increased lethality of synthetic opioids. The 2017 Narcan distribution coincided with a modest slowing in growth, but the 2020 COVID onset disrupted this trend, with deaths spiking across nearly all age groups. By 2021–2022, the rollout of COVID vaccines helped stabilize mortality among older adults, though younger and middle‑aged groups continued to bear a disproportionate burden. By 2022–2023, despite new opioid interventions like Narcan vending machines and OTC Narcan availability, these measures were not sufficient to offset the combined opioid and pandemic pressures.
Deaths by Race/Ethnicity: The race/ethnicity plot highlights disparities across groups. Following the 2014 fentanyl surge, mortality rose steeply among White populations, but by the 2020 COVID onset, Black and Hispanic populations also experienced sharp increases. The 2021–2022 vaccine rollout contributed to declines in COVID‑related mortality, particularly among groups with higher vaccination coverage, but disparities persisted where access and uptake were uneven. The 2022–2023 opioid interventions appear to have stabilized deaths somewhat among White populations, while minority groups continued to face structural disadvantages and uneven benefits from interventions.
Deaths by Sex: The sex‑based plot shows consistently higher mortality among males compared to females across all years. The 2014 fentanyl surge widened this gap, and while the 2017 Narcan distribution provided some relief, the 2020 COVID onset again amplified male mortality. The 2021–2022 vaccine rollout reduced COVID‑related deaths in both sexes, but the male–female disparity remained pronounced due to higher baseline male mortality. By 2022–2023, interventions like Narcan vending machines and OTC Narcan helped slow growth, but sex‑based differences persisted, underscoring the need for sex‑specific, targeted strategies.
4.4 Statistical Modeling
Prepare the Data Set for Statistical Modeling: Now that the data have been visually contextualized, the next step is to formally test demographic differences using regression models. To prepare the dataset for modeling, contextual columns are merged into mortality_summary, ensuring intervention information is integrated alongside demographic mortality data. The process begins by selecting only the core variables—year, sex, race/ethnicity, age category, and total deaths—before joining contextual variables from mortality_full, including opioid notes, vaccine dose counts, and vaccination notes, with one unique row per year. Categorical variables such as sex, race/ethnicity, and age category are converted to factors so they can be properly handled in regression models. To ensure interpretable comparisons, baselines are explicitly set: Male for sex, White (NH) for race/ethnicity, and 25–44 years for age category. Binary indicators are then created for key milestones: opioid interventions such as the fentanyl surge (2014+), Narcan distribution (2017+), vending machines (2022+), and OTC Narcan (2023+), as well as COVID interventions including onset (2020+), vaccine rollout (based on dose data), and booster campaigns (2021+). This structure provides a clean dataset with demographic and contextual predictors ready for regression analysis, allowing formal statistical testing of differences across groups and the impacts of interventions.
# Step 1: Start from the original mortality_summary (demographics + deaths only)mortality_summary <- mortality_summary %>%# Keep only the core demographic and death count columns dplyr::select(year, sex, race_ethnicity, age_category, total_deaths) %>%# Step 2: Join in contextual variables cleanly from mortality_fullleft_join( mortality_full %>% dplyr::select(year, opioid_notes, covid_vaccine_doses_philadelphia, vax_notes) %>%group_by(year) %>%summarise(opioid_notes =first(opioid_notes),covid_vaccine_doses_philadelphia =first(covid_vaccine_doses_philadelphia),vax_notes =first(vax_notes),.groups ="drop" ),by ="year" ) %>%# Step 3: Convert categorical variables to factorsmutate(sex =factor(sex),race_ethnicity =factor(race_ethnicity),age_category =factor(age_category) ) %>%# Step 4: Create binary indicators for interventionsmutate(fentanyl_surge =ifelse(year >=2014, 1, 0),narcan_distribution =ifelse(year >=2017, 1, 0),narcan_vending_machines =ifelse(year >=2022, 1, 0),otc_narcan =ifelse(year >=2023, 1, 0),covid_onset =ifelse(year >=2020, 1, 0),vaccine_rollout =ifelse(!is.na(covid_vaccine_doses_philadelphia), 1, 0),booster_campaigns =ifelse(year >=2021, 1, 0) )# Step 5: Set baselines for categorical predictorsmortality_summary$sex <-relevel(mortality_summary$sex, ref ="Male")mortality_summary$race_ethnicity <-relevel(mortality_summary$race_ethnicity, ref ="White (NH)")mortality_summary$age_category <-relevel(mortality_summary$age_category, ref ="25-44")
Linear Regression (Exploratory), Interpreting Linear Regression of Mortality Counts with Demographics and Public Health Interventions: This exploratory linear regression examines associations between mortality counts, demographic factors, and public health interventions. Although linear regression is not ideal for count data, it provides a quick way to identify broad trends. The model explains about 63% of the variation in total deaths (R² = 0.635, Adjusted R² = 0.629), which is fairly strong for social and health data.
Demographic predictors are highly significant. Female shows a large negative coefficient relative to the baseline Male, indicating fewer deaths among females compared to males. Race and ethnicity groups are interpreted relative to White (NH): Asian/PI, Hispanic, and Multiracial all have strong negative coefficients, while Black (NH) shows a positive and significant coefficient, indicating higher deaths compared to White (NH). Age categories are interpreted relative to the baseline 25–44 years. Older groups such as 45–64 and 65+ are associated with substantially higher death counts, while younger groups like 5–14 and 15–24 show significant negative associations relative to the baseline.
The year variable has a very small, non‑significant coefficient, suggesting no linear trend in deaths once demographics are accounted for. Intervention indicators show no significant effects in this setup: opioid milestones (fentanyl surge, Narcan distribution, vending machines, OTC Narcan) and COVID interventions (onset, vaccine rollout, booster campaigns) all have coefficients close to zero or non‑significant p‑values. Vaccine rollout was dropped due to perfect collinearity with other predictors, meaning its effect could not be separated.
Overall, the results indicate that demographics drive most of the variation in mortality counts, while intervention flags coded as simple binary indicators do not capture significant effects in this linear framework. This highlights the limitations of linear regression for count data and the need for more nuanced modeling approaches.
library(dplyr)# Step 1: Fit a linear regression model# Purpose: Exploratory look at associations between demographics, interventions, and mortality countsmodel <-lm( total_deaths ~ sex + race_ethnicity + age_category + year + fentanyl_surge + narcan_distribution + narcan_vending_machines + otc_narcan + covid_onset + vaccine_rollout + booster_campaigns,data = mortality_summary %>%mutate(sex =factor(sex),race_ethnicity =factor(race_ethnicity),age_category =factor(age_category) ))# Step 2: Summarize the model outputsummary(model)
Interpreting Poisson Regression of Mortality Counts with Demographics and Public Health Interventions: Interpreting Poisson Regression of Mortality Counts with Demographics and Public Health Interventions: Extracting rate ratios from a Poisson regression in R allows results to be presented in a clear and interpretable format. The coefficients produced by the model are on the log scale, which can be difficult to interpret directly. By exponentiating these values, they are converted into rate ratios that represent multiplicative effects relative to the chosen baselines. In this analysis, Male is the baseline for sex, White (NH) is the baseline for race/ethnicity, and 25–44 years is the baseline for age category. Rate ratios greater than 1 indicate higher expected deaths relative to the baseline, while values less than 1 indicate lower expected deaths.
Demographic predictors show strong and highly significant associations. Females have fewer expected deaths compared to males. Asian/PI, Hispanic, and Multiracial groups show strong protective effects compared to White (NH), while Black (NH) shows significantly higher expected deaths compared to White (NH). Age categories are interpreted relative to adults aged 25–44: older groups (45–64, 65+) are associated with ~3× and ~9× higher deaths respectively, and the “all ages” group shows ~14× higher deaths. Younger groups (0–4, 5–14, 15–24) show strong protective effects. The year variable indicates a small but statistically significant decline of about 0.6% fewer deaths per year.
Intervention indicators show mixed results. Fentanyl surge and Narcan distribution are associated with small increases in deaths (~3%), while vending machines and OTC Narcan are associated with reductions (~5% and ~11% fewer deaths respectively). COVID onset corresponds to a substantial increase (~29% higher deaths), while booster campaigns are linked to a ~9% reduction. Vaccine rollout was dropped due to collinearity with other COVID variables.
Overall, the Poisson regression highlights the dominant role of demographics in mortality variation, while interventions show measurable but smaller associations. Presenting the results in a tidy table with explicit variable names and polished formatting improves readability and makes the findings easier to communicate.
# Step 1: Fit a Poisson regression modelglm_model <-glm( total_deaths ~ sex + race_ethnicity + age_category + year + fentanyl_surge + narcan_distribution + narcan_vending_machines + otc_narcan + covid_onset + vaccine_rollout + booster_campaigns,family =poisson(link ="log"),data = mortality_summary %>%mutate(sex =factor(sex),race_ethnicity =factor(race_ethnicity),age_category =factor(age_category) ))# Step 2: Summarize the model outputsummary(glm_model)
Extracting Rate Ratios from Poisson Regression: Extracting rate ratios from a Poisson regression in R allows results to be presented in a clear and interpretable format. The coefficients produced by the model are on the log scale, which can be difficult to interpret directly. By exponentiating these values, they are converted into rate ratios that represent multiplicative effects relative to the chosen baselines. In this analysis, Male is the baseline for sex, White (NH) is the baseline for race/ethnicity, and 25–44 years is the baseline for age category. Confidence intervals are included to show the range of plausible effects, making it possible to assess statistical precision and determine whether predictors differ significantly from 1, which represents no effect.
Rate ratios greater than 1 indicate higher expected deaths compared to males, White (NH), or adults aged 25–44, while values less than 1 indicate lower expected deaths compared to those same baselines. Females show significantly fewer deaths compared to males. Asian/PI, Hispanic, and Multiracial groups show strong protective effects compared to White (NH), while Black (NH) shows significantly higher expected deaths compared to White (NH). Age categories are interpreted relative to adults aged 25–44: older groups (45–64, 65+) are associated with ~3× and ~9× higher deaths respectively, and the “all ages” group shows ~14× higher deaths. Younger groups (0–4, 5–14, 15–24) show strong protective effects. The year variable indicates a small but statistically significant decline of about 0.6% fewer deaths per year.
Intervention indicators show mixed results. Fentanyl surge and Narcan distribution are associated with small increases in deaths (~3%), while vending machines and OTC Narcan are associated with reductions (~5% and ~11% fewer deaths respectively). COVID onset corresponds to a substantial increase (~29% higher deaths), while booster campaigns are linked to a ~9% reduction. Vaccine rollout was dropped due to collinearity with other COVID variables.
Together, these steps translate the log‑scale output of Poisson regression into interpretable measures that highlight the relative impact of demographics and interventions on mortality counts.
library(broom)library(tibble)library(knitr)# Step 1: Extract tidy results with exponentiated coefficients and confidence intervalsexp_table <-tidy(glm_model, exponentiate =TRUE, conf.int =TRUE)# Step 2: Convert to tibble for clear variable namesexp_table <-as_tibble(exp_table)# Step 3: Format the table for polished readabilitykable(exp_table, digits =3, caption ="Rate Ratios from Poisson Regression with 95% Confidence Intervals")
Rate Ratios from Poisson Regression with 95% Confidence Intervals
term
estimate
std.error
statistic
p.value
conf.low
conf.high
(Intercept)
75583763.495
1.961
9.252
0
1620083.420
3.526348e+09
sexAll sexes
1.937
0.001
459.852
0
1.931
1.942000e+00
sexFemale
0.938
0.002
-38.146
0
0.935
9.410000e-01
race_ethnicityAll races/ethnicities
2.392
0.002
567.520
0
2.385
2.399000e+00
race_ethnicityAsian/PI (NH)
0.056
0.006
-510.890
0
0.056
5.700000e-02
race_ethnicityBlack (NH)
1.133
0.002
70.291
0
1.129
1.136000e+00
race_ethnicityHispanic
0.147
0.004
-531.613
0
0.146
1.480000e-01
race_ethnicityMultiracial (NH)
0.005
0.020
-266.208
0
0.005
5.000000e-03
age_category0-4
0.141
0.009
-215.564
0
0.138
1.430000e-01
age_category15-24
0.194
0.008
-208.054
0
0.191
1.970000e-01
age_category45-64
3.335
0.004
337.410
0
3.312
3.359000e+00
age_category5-14
0.018
0.028
-141.571
0
0.017
1.900000e-02
age_category65
9.207
0.003
673.299
0
9.148
9.267000e+00
age_categoryAll ages
13.945
0.003
812.817
0
13.857
1.403400e+01
year
0.994
0.001
-6.188
0
0.992
9.960000e-01
fentanyl_surge
1.029
0.003
8.986
0
1.022
1.035000e+00
narcan_distribution
1.032
0.003
9.134
0
1.025
1.039000e+00
narcan_vending_machines
0.949
0.003
-16.969
0
0.944
9.550000e-01
otc_narcan
0.892
0.003
-38.404
0
0.887
8.970000e-01
covid_onset
1.286
0.003
83.499
0
1.278
1.293000e+00
vaccine_rollout
NA
NA
NA
NA
NA
NA
booster_campaigns
0.911
0.003
-31.693
0
0.905
9.160000e-01
Rate Ratios from Poisson Regression of Mortality Counts: Rate ratios from the Poisson regression of mortality counts provide an accessible way to interpret the effects of demographics and interventions. A rate ratio represents the multiplicative effect on expected deaths, with values greater than 1 indicating higher expected deaths and values less than 1 indicating lower expected deaths relative to the baseline. Confidence intervals provide the 95% bounds for these estimates, while p‑values indicate statistical significance. In this analysis, Male is the baseline for sex, White (NH) is the baseline for race/ethnicity, and 25–44 years is the baseline for age category.
The results show that females have significantly fewer expected deaths compared to males. Other race/ethnicity groups show strong differences compared to White (NH): Asian/PI (NH), Hispanic, and Multiracial groups all show strong protective effects, while Black (NH) shows about 13% higher expected deaths compared to White (NH). Age is a dominant factor, with those aged 45–64 experiencing ~3× higher expected deaths, those aged 65+ experiencing ~9× higher deaths, and the “all ages” group showing ~14× higher deaths compared to adults aged 25–44. Younger groups (0–4, 5–14, 15–24) show strong protective effects. The year variable indicates a small but statistically significant decline of about 0.6% fewer expected deaths per year.
Intervention effects are mixed: the fentanyl surge and Narcan distribution coincide with ~3% higher expected deaths, while later interventions such as vending machines and OTC Narcan are associated with reductions of ~5% and ~11% respectively. COVID onset corresponds to a ~29% increase in expected deaths, while booster campaigns are linked to a ~9% reduction. Vaccine rollout was dropped due to collinearity with other COVID variables.
Overall, demographics dominate mortality variation, while interventions show measurable but smaller associations. Presenting the results in a structured table with clear variable names, confidence intervals, and significance flags makes the findings easier to interpret and communicate.
Rate Ratios from Poisson Regression with 95% Confidence Intervals
Predictor
RateRatio
LowerCI
UpperCI
PValue
Significance
Group
covid_onset
1.286
1.278
1.293000e+00
0
Yes
COVID Interventions
vaccine_rollout
NA
NA
NA
NA
NA
COVID Interventions
booster_campaigns
0.911
0.905
9.160000e-01
0
Yes
COVID Interventions
sexAll sexes
1.937
1.931
1.942000e+00
0
Yes
Demographics
sexFemale
0.938
0.935
9.410000e-01
0
Yes
Demographics
race_ethnicityAll races/ethnicities
2.392
2.385
2.399000e+00
0
Yes
Demographics
race_ethnicityAsian/PI (NH)
0.056
0.056
5.700000e-02
0
Yes
Demographics
race_ethnicityBlack (NH)
1.133
1.129
1.136000e+00
0
Yes
Demographics
race_ethnicityHispanic
0.147
0.146
1.480000e-01
0
Yes
Demographics
race_ethnicityMultiracial (NH)
0.005
0.005
5.000000e-03
0
Yes
Demographics
age_category0-4
0.141
0.138
1.430000e-01
0
Yes
Demographics
age_category15-24
0.194
0.191
1.970000e-01
0
Yes
Demographics
age_category45-64
3.335
3.312
3.359000e+00
0
Yes
Demographics
age_category5-14
0.018
0.017
1.900000e-02
0
Yes
Demographics
age_category65
9.207
9.148
9.267000e+00
0
Yes
Demographics
age_categoryAll ages
13.945
13.857
1.403400e+01
0
Yes
Demographics
fentanyl_surge
1.029
1.022
1.035000e+00
0
Yes
Opioid Interventions
narcan_distribution
1.032
1.025
1.039000e+00
0
Yes
Opioid Interventions
narcan_vending_machines
0.949
0.944
9.550000e-01
0
Yes
Opioid Interventions
otc_narcan
0.892
0.887
8.970000e-01
0
Yes
Opioid Interventions
(Intercept)
75583763.495
1620083.420
3.526348e+09
0
Yes
Other
year
0.994
0.992
9.960000e-01
0
Yes
Year Trend
Interpreting Negative Binomial Regression of Mortality Counts with Demographics and Public Health Interventions: Negative Binomial regression is a generalized linear model designed for count data when the variance exceeds the mean, a condition known as overdispersion. Unlike Poisson regression, which assumes the mean and variance are equal, the negative binomial model introduces a dispersion parameter (theta) that adjusts for extra variability, making it more robust for real‑world data such as mortality counts. In this model, coefficients are expressed on the log scale, and exponentiating them yields rate ratios that represent multiplicative effects on expected deaths. Confidence intervals provide bounds for these estimates, while the dispersion parameter confirms whether overdispersion is present. The model fit is strong, with a theta of 5.83 (SE = 0.25), residual deviance of 1,263.7 on 1,186 degrees of freedom, and an AIC of 15,629, all indicating better performance than the Poisson regression.
Demographic predictors dominate. Females show significantly fewer expected deaths compared to the baseline Male. Race and ethnicity categories show varied associations compared to the baseline White (NH): Asian/PI (NH), Hispanic, and Multiracial (NH) groups all have strong negative associations, while Black (NH) shows significantly higher expected deaths. Age categories are now interpreted relative to 25–44 years, with older groups (45–64, 65+) exhibiting large positive coefficients — the 65+ category is associated with about 9 times higher expected deaths compared to adults aged 25–44, and the “all ages” group shows about 14 times higher expected deaths. Younger groups (5–14, 15–24) show strong negative effects relative to this baseline. The year variable is not significant, suggesting no clear linear trend once demographics are controlled.
Intervention indicators show mixed results: fentanyl surge and Narcan distribution are not significant, vending machines show a borderline reduction, and OTC Narcan demonstrates a significant protective effect with about 16% fewer deaths. COVID onset corresponds to a strong and significant increase of about 25% in expected deaths, while booster campaigns are not significant. Vaccine rollout was dropped due to collinearity with other COVID variables.
Overall, the negative binomial regression confirms that demographics are the primary drivers of mortality counts, while OTC Narcan and COVID onset show clear, significant effects. This model handles overdispersion better than Poisson, providing more reliable inference for mortality data, and will be used for the remainder of the analysis.
# Step 1: Fit the negative binomial regression# glm.nb adds a dispersion parameter to account for overdispersion in count datanb_model <- MASS::glm.nb( total_deaths ~ sex + race_ethnicity + age_category + year + fentanyl_surge + narcan_distribution + narcan_vending_machines + otc_narcan + covid_onset + vaccine_rollout + booster_campaigns,data = mortality_summary %>%mutate(sex =factor(sex),race_ethnicity =factor(race_ethnicity),age_category =factor(age_category) ))# Step 2: View model summarysummary(nb_model)
Call:
MASS::glm.nb(formula = total_deaths ~ sex + race_ethnicity +
age_category + year + fentanyl_surge + narcan_distribution +
narcan_vending_machines + otc_narcan + covid_onset + vaccine_rollout +
booster_campaigns, data = mortality_summary %>% mutate(sex = factor(sex),
race_ethnicity = factor(race_ethnicity), age_category = factor(age_category)),
init.theta = 5.826945797, link = log)
Coefficients: (1 not defined because of singularities)
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.284304 39.966837 0.082 0.934507
sexAll sexes 0.520940 0.029619 17.588 < 2e-16 ***
sexFemale -0.450041 0.030888 -14.570 < 2e-16 ***
race_ethnicityAll races/ethnicities 1.167404 0.038715 30.154 < 2e-16 ***
race_ethnicityAsian/PI (NH) -2.689073 0.045601 -58.969 < 2e-16 ***
race_ethnicityBlack (NH) 0.528334 0.038807 13.614 < 2e-16 ***
race_ethnicityHispanic -1.266530 0.040818 -31.029 < 2e-16 ***
race_ethnicityMultiracial (NH) -4.989162 0.059604 -83.705 < 2e-16 ***
age_category0-4 -1.966104 0.048550 -40.497 < 2e-16 ***
age_category15-24 -1.692627 0.047975 -35.281 < 2e-16 ***
age_category45-64 1.133390 0.042689 26.550 < 2e-16 ***
age_category5-14 -4.067976 0.068555 -59.339 < 2e-16 ***
age_category65 2.209602 0.042134 52.442 < 2e-16 ***
age_categoryAll ages 2.646069 0.042065 62.904 < 2e-16 ***
year 0.001296 0.019859 0.065 0.947974
fentanyl_surge -0.011471 0.064053 -0.179 0.857875
narcan_distribution 0.028525 0.069683 0.409 0.682279
narcan_vending_machines -0.090833 0.065434 -1.388 0.165083
otc_narcan -0.173794 0.062245 -2.792 0.005237 **
covid_onset 0.222830 0.065047 3.426 0.000613 ***
vaccine_rollout NA NA NA NA
booster_campaigns 0.017979 0.065471 0.275 0.783617
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for Negative Binomial(5.8269) family taken to be 1)
Null deviance: 26495.4 on 1206 degrees of freedom
Residual deviance: 1263.7 on 1186 degrees of freedom
AIC: 15629
Number of Fisher Scoring iterations: 1
Theta: 5.827
Std. Err.: 0.251
2 x log-likelihood: -15585.079
# Step 3: Exponentiate coefficients to get rate ratios with confidence intervalsexp_table_nb <-cbind(Estimate =coef(nb_model), # log-scale coefficientsRateRatio =exp(coef(nb_model)), # exponentiated coefficientsLowerCI =exp(confint(nb_model)[,1]), # lower bound of CIUpperCI =exp(confint(nb_model)[,2]) # upper bound of CI)
Waiting for profiling to be done...
Waiting for profiling to be done...
Summarizing Rate Ratios from Negative Binomial Regression of Mortality Counts: This step presents the results of the Negative Binomial regression in a clean and interpretable table of rate ratios. The tidy output from the model is reformatted to highlight each predictor, its estimated multiplicative effect on expected deaths, the 95% confidence interval bounds, and the associated p‑value. By rounding values for readability, the table makes it easy to see which predictors are statistically significant and the magnitude of their effects.
In this analysis, Male is the baseline for sex, White (NH) is the baseline for race/ethnicity, and 25–44 years is the baseline for age category. Demographic variables show strong and highly significant associations: females are interpreted relative to males and show significantly fewer expected deaths. Race/ethnicity groups are interpreted relative to White (NH): Asian/PI, Hispanic, and Multiracial groups show strong negative associations, while Black (NH) shows significantly higher expected deaths compared to White (NH). Age categories are now compared against adults aged 25–44, with older groups (45–64, 65+) showing large positive effects — the 65+ group is associated with about 9 times higher expected deaths compared to 25–44, and the “All ages” group shows about 14 times higher expected deaths.Younger groups (5–14, 15–24) show strong negative associations relative to this baseline.
Interventions such as OTC Narcan and COVID onset also emerge as important factors, with OTC Narcan associated with about 16% fewer deaths and COVID onset linked to a 25% increase in expected deaths. Other interventions, including fentanyl surge, Narcan distribution, vending machines, and booster campaigns, are not statistically significant in this model. Presenting the results in this structured format allows for straightforward interpretation: rate ratios greater than one indicate higher expected deaths compared to the baselines (Male, White (NH), 25–44), values less than one indicate lower expected deaths compared to those baselines, and confidence intervals provide the range of plausible effects. This table serves as a clear summary of the regression findings, emphasizing the dominant role of demographics and the measurable impact of select interventions on mortality counts.
library(broom)library(dplyr)# Tidy NB regression output with exponentiationtidy_nb <- broom::tidy(nb_model, conf.int =TRUE, exponentiate =TRUE)# Inspect column namescolnames(tidy_nb)
Visualizing Mortality Predictors with a Forest Plot: The forest plot provides a visual summary of the rate ratios from the Negative Binomial regression, showing the effects of demographics and interventions on mortality counts. Each dot represents the estimated rate ratio for a predictor, while the horizontal bars display the 95% confidence intervals. The red dashed vertical line at 1 marks the null effect, meaning no change in deaths. Predictors plotted to the left of 1 are associated with fewer deaths, while those to the right are associated with more deaths. The log scale on the x‑axis allows both very large and very small effects to be compared side by side.
In this analysis, Male is the baseline for sex, White (NH) is the baseline for race/ethnicity, and 25–44 years is the baseline for age category. The plot highlights strong protective effects for females compared to males, and for Asian/PI, Hispanic, and Multiracial groups compared to White (NH). In contrast, Black (NH) shows significantly higher expected deaths compared to White (NH), appearing to the right of the null line. Age categories also show clear differences: 45–64 and 65+ groups stand out as strong risk factors compared to 25–44, with about 3 times and 9 times higher expected deaths respectively, while the “all ages” group shows about 14 times higher expected deaths.Younger groups (5–14, 15–24) are plotted far to the left, reflecting strong protective effects.
Among interventions, OTC Narcan is associated with a significant reduction (~16% fewer deaths), while COVID onset corresponds to a substantial increase (~25% higher deaths). Other predictors, including the year trend, fentanyl surge, Narcan distribution, vending machines, and booster campaigns, do not show statistically significant effects.
Color‑coding the predictors by group further enhances readability, making it easy to distinguish between demographics, opioid interventions, and COVID interventions. Overall, the forest plot provides an intuitive visualization of which predictors have meaningful associations with mortality counts, emphasizing the dominant role of demographics and the measurable impact of select interventions.
# Load librarieslibrary(ggplot2)library(dplyr)library(broom)# Start from tidy NB regression output with confidence intervalsdf <- broom::tidy(nb_model, conf.int =TRUE, exponentiate =TRUE) %>% dplyr::select(term, estimate, conf.low, conf.high, p.value) %>% dplyr::rename(Predictor = term,RateRatio = estimate,LowerCI = conf.low,UpperCI = conf.high,PValue = p.value )# Remove baseline categories (they always equal 1 and add clutter)df <- df %>%filter(!Predictor %in%c("sexMale", "race_ethnicityWhite (NH)", "age_category25-44"))# Optional: relabel predictors for readabilitydf$Predictor <-recode(df$Predictor,"sexFemale"="Female","race_ethnicityAsian/PI (NH)"="Asian/PI (NH)","race_ethnicityBlack (NH)"="Black (NH)","race_ethnicityHispanic"="Hispanic","race_ethnicityMultiracial (NH)"="Multiracial (NH)","age_category5-14"="Age 5–14","age_category15-24"="Age 15–24","age_category45-64"="Age 45–64","age_category65+"="Age 65+")# Order predictors for plottingdf$Predictor <-factor(df$Predictor, levels =rev(unique(df$Predictor)))# Forest plotggplot(df, aes(x = RateRatio, y = Predictor)) +geom_point(size =3, color ="lightblue") +geom_errorbarh(aes(xmin = LowerCI, xmax = UpperCI),height =0.3, size =1, color ="black") +geom_vline(xintercept =1, linetype ="dashed", color ="red") +scale_x_log10() +coord_cartesian(xlim =c(0.001, 200)) +# extend lower bound to show very small valueslabs(title ="Forest Plot of Predictors of Mortality",x ="Rate Ratio (log scale)", y ="") +theme_minimal(base_size =12)
Warning: `geom_errorbarh()` was deprecated in ggplot2 4.0.0.
ℹ Please use the `orientation` argument of `geom_errorbar()` instead.
`height` was translated to `width`.
4.5 Forest Plot of Intervention Effects on Mortality
This forest plot focuses on the effects of opioid and COVID interventions on mortality. The results show that OTC Narcan was associated with a significant protective effect, with a rate ratio of 0.84 (95% CI: 0.74–0.95), corresponding to roughly 16 percent fewer deaths. In contrast, the onset of COVID was linked to a significant harmful effect, with a rate ratio of 1.25 (95% CI: 1.10–1.42), indicating about 25 percent more deaths. Other interventions, including the fentanyl surge, Narcan distribution, vending machines, and booster campaigns, had confidence intervals that crossed 1, suggesting their effects were not statistically significant in this negative binomial model. Overall, the plot provides an intervention‑focused view of how opioid and COVID policies aligned with changes in mortality risk
library(ggplot2)# Create a data frame of intervention predictors onlydf_interventions <-data.frame(Predictor =c("fentanyl_surge","narcan_distribution","narcan_vending","otc_narcan","covid_onset","booster_campaigns"),RateRatio =c(0.989,1.029,0.913,0.840,1.250,1.018),LowerCI =c(0.871,0.898,0.803,0.744,1.100,0.895),UpperCI =c(1.122,1.179,1.039,0.949,1.421,1.158))# Order predictors for plottingdf_interventions$Predictor <-factor(df_interventions$Predictor,levels =rev(df_interventions$Predictor))# Forest plotggplot(df_interventions, aes(x = RateRatio, y = Predictor)) +geom_point(size =3, color ="darkgreen") +geom_errorbarh(aes(xmin = LowerCI, xmax = UpperCI), height =0.2) +geom_vline(xintercept =1, linetype ="dashed", color ="red") +scale_x_log10(limits =c(0.7, 1.5)) +# zoom in for claritylabs(title ="Forest Plot of Intervention Effects on Mortality",x ="Rate Ratio (log scale)", y ="") +theme_minimal(base_size =12)
`height` was translated to `width`.
Demographic Variation in COVID‑19 Vaccination Effectiveness: The negative binomial regression with interaction terms for vaccination years 2021–2022 and demographic subgroups was designed to test the hypothesis that the effectiveness of COVID‑19 vaccination in reducing mortality varied significantly by race, age, and gender. Using the combined 2021–2022 indicator is a good choice because it captures the full vaccination rollout period in one variable, avoids collinearity with separate year flags, and provides a stable, interpretable measure of overall vaccine impact on mortality counts. While the overall effect of vaccination across 2021–2022 was not statistically significant, subgroup analyses revealed important differences: Hispanic populations showed significantly weaker protective effects (RR ≈ 1.35, p = 0.002), Asian/PI groups also demonstrated elevated risk (RR ≈ 1.34, p = 0.004), and Black populations trended toward disparity without reaching conventional significance (RR ≈ 1.21, p = 0.055). Multiracial groups showed borderline evidence of elevated risk (RR ≈ 1.33, p = 0.07). Age interactions did not yield significant subgroup differences, and female sex remained protective overall (RR ≈ 0.61, p < 0.001), though vaccination did not significantly alter that effect. Taken together, these findings support the hypothesis that vaccination effectiveness was not uniform, but instead varied across demographic subgroups, underscoring the importance of equity‑focused approaches in evaluating vaccine impact on mortality.
Equity Impacts of COVID Vaccination and Opioid Interventions on Mortality Disparities: This analysis tested whether disparities narrowed after interventions, specifically COVID‑19 vaccination in 2021–2022 and opioid interventions in 2017–2022, across sex, age, and race/ethnicity subgroups. A negative binomial regression was fit using the mortality_summary dataset, excluding ages 0–4. Binary indicators were created for vaccination (vax_any: 2021–2022) and opioid interventions (opioid_any: 2017–2022), and these were interacted with sex, race/ethnicity, and age categories, with baselines set as Male, White (NH), and ages 25–44. Covariates included fentanyl surge, COVID onset, and year to account for contextual mortality trends.
The results showed that vaccination had no significant overall impact, while opioid interventions were associated with a modest increase in mortality. Sex interactions revealed no evidence of narrowing or widening disparities. Race and ethnicity interactions, however, highlighted important variation: vaccination effects differed significantly for Asian/PI, Hispanic, and Multiracial groups compared to White (NH), suggesting uneven protective benefits. For opioid interventions, Multiracial groups showed a significant negative interaction, indicating narrowed disparities, while other race/ethnicity effects were non‑significant. Age interactions revealed no significant differences for vaccination, but opioid interventions showed borderline evidence of narrowing disparities among younger (15–24) and older (45–64, 65+) groups. Contextual covariates behaved as expected, with COVID onset increasing mortality, year trends showing gradual declines, and sex and race main effects confirming known disparities such as female protection, Black excess mortality, and protective effects for Asian/PI and Hispanic populations.
Overall, the hypothesis that disparities narrowed after interventions is partially supported. Vaccination impacts varied across race and ethnicity but did not show clear narrowing by sex or age, while opioid interventions provided some evidence of narrowing for Multiracial groups and possible narrowing for younger and older age categories. These findings suggest that intervention impacts were not uniform across demographic groups, with the strongest equity signals emerging in race and ethnicity interactions, while sex and age disparities remained largely unchanged. Future work should refine subgroup interaction models to better quantify equity impacts and identify where interventions most effectively reduce disparities.
Evaluating Age‑Specific Impacts of Narcan Interventions (2017–2022): To test the hypothesis that Narcan’s impact on reducing mortality is greater in younger groups, I constructed a negative binomial regression using the mortality_summary dataset. The code created binary indicators for Narcan interventions in 2017, 2022, and a combined indicator for 2017–2022 (narcan_any). These indicators were interacted with age categories (baseline: 25–44) to assess whether Narcan’s protective effects varied across demographic subgroups. Additional covariates included sex, race/ethnicity, fentanyl surge, COVID onset, vaccine rollout, booster campaigns, and year, with categorical predictors re‑leveled to set meaningful baselines.
The regression results show that the baseline group (ages 25–44) had a significant positive Narcan effect (Estimate = 0.33, p < 0.001). Interaction terms revealed that older groups (45–64 and 65+) experienced significantly weaker Narcan impacts compared to the baseline (Estimates = –0.20, p = 0.026; –0.23, p = 0.009). The “All ages” aggregate also showed a weaker effect (Estimate = –0.22, p = 0.012). By contrast, younger groups (15–24 and 5–14) did not differ significantly from the baseline, meaning their Narcan impact was statistically similar to 25–44 rather than stronger. Year‑specific indicators for 2017 and 2022 showed no significant main effects or interactions, suggesting that the combined 2017–2022 measure captured the more consistent pattern. Other covariates behaved as expected: COVID onset increased mortality (Estimate = 0.30, p < 0.001), females were protective relative to males, and Black populations experienced excess mortality.
In summary, the analysis indicates that Narcan’s impact was strongest in the baseline 25–44 group, weaker in older groups, and similar in younger groups, thereby not supporting the original hypothesis that Narcan’s protective effect is greater in younger populations. Instead, the findings highlight that Narcan interventions reduced mortality most clearly among adults aged 25–44, underscoring the need for further subgroup‑focused evaluation to understand age‑specific dynamics.
# Load librarieslibrary(MASS)library(broom)library(dplyr)library(ggplot2)# Step A: Exclude age group 0–4mortality_summary <- mortality_summary %>%filter(age_category !="0-4") %>%# remove 0–4 from analysismutate(narcan_2017 =ifelse(year ==2017, 1, 0),narcan_2022 =ifelse(year ==2022, 1, 0),narcan_any =ifelse(year %in%2017:2022, 1, 0) # combined indicator for all years 2017–2022 )# Step B: Fit Negative Binomial regression with Narcan years × age categoriesnb_narcan_years <-glm.nb( total_deaths ~ narcan_any * age_category + narcan_2017 * age_category + narcan_2022 * age_category + sex + race_ethnicity + fentanyl_surge + covid_onset + vaccine_rollout + booster_campaigns + year,data = mortality_summary)# Step C: Summarize resultssummary(nb_narcan_years)
# Step D: Extract tidy coefficients with exponentiated values (rate ratios)narcan_year_results <-tidy(nb_narcan_years, exponentiate =TRUE, conf.int =TRUE)# Step E: Filter to Narcan-related terms (main effects + interactions)narcan_year_effects <- narcan_year_results %>%filter(grepl("narcan_", term))# Step F: Forest plot of ALL subgroup-specific Narcan effects (2017–2022, excluding 0–4)ggplot(narcan_year_effects, aes(x = estimate, y = term)) +geom_point(color ="purple", size =3) +geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), height =0.2) +geom_vline(xintercept =1, linetype ="dashed", color ="red") +scale_x_log10() +labs(title ="Narcan Effects by Age Groups (Excluding 0–4)",x ="Rate Ratio (log scale)",y ="Predictor (Interaction Term)") +theme_minimal()
`height` was translated to `width`.
Title: Unified Model of Demographic Disparities in Mortality at COVID Onset
Definition: This Negative Binomial regression includes interaction terms for sex, race/ethnicity, and age category with COVID onset. By modeling all three simultaneously, we can estimate how the pandemic’s effect on mortality counts differed across demographic groups, while controlling for other interventions and time trends.
🔹 Why This Unified Model Matters
Single framework: All disparities are estimated in one regression, ensuring consistency.
Interactions: Show whether COVID onset increased deaths differently across sex, race, and age groups.
Comparability: Results can be directly compared across demographics.
🔹 How to Use the Output
The tidy table (rate_ratio_table_all) contains all predictors, including the interaction terms.
Subsets (sex_covid_effects, race_covid_effects, age_covid_effects) isolate the relevant rows for each demographic disparity.
You can feed each subset into your existing forest plot code to generate three separate plots (sex, race, age).
🔹 Key Takeaway
COVID onset did not affect all demographic groups equally.
The unified model reveals sex, race, and age disparities side‑by‑side, highlighting which populations bore the greatest mortality burden at the start of the pandemic.
Forest plots make these differences visually clear, combining statistical rigor with intuitive interpretation.
This way, you only fit one model and then reuse the subsets for your plotting code.
library(MASS)library(broom)library(dplyr)library(knitr)library(gt)# Step 0: Relevel age_category so "25–44" is the reference groupmortality_summary <- mortality_summary %>%mutate(age_category =relevel(factor(age_category), ref ="15") )# Step 1: Fit NB regression with all three interactionsnb_model_all <- MASS::glm.nb( total_deaths ~ sex*covid_onset + race_ethnicity*covid_onset + age_category*covid_onset + year + fentanyl_surge + narcan_distribution + narcan_vending + otc_narcan + vaccine_rollout + booster_campaigns,data = mortality_summary)# Step 2: Tidy output with exponentiationtidy_nb_all <- broom::tidy(nb_model_all, conf.int =TRUE, exponentiate =TRUE)# Step 3: Format tablerate_ratio_table_all <- tidy_nb_all %>% dplyr::select(term, estimate, conf.low, conf.high, p.value) %>% dplyr::rename(Predictor = term,RateRatio = estimate,LowerCI = conf.low,UpperCI = conf.high,PValue = p.value ) %>% dplyr::mutate(RateRatio =round(RateRatio, 3),LowerCI =round(LowerCI, 3),UpperCI =round(UpperCI, 3),PValue =signif(PValue, 3),PercentChange =round((RateRatio -1) *100, 1) )# Step 4: Create subsets for each disparitysex_covid_effects <- rate_ratio_table_all %>% dplyr::filter(grepl("sex.*:covid_onset", Predictor))race_covid_effects <- rate_ratio_table_all %>% dplyr::filter(grepl("race_ethnicity.*:covid_onset", Predictor))age_covid_effects <- rate_ratio_table_all %>% dplyr::filter(grepl("age_category.*:covid_onset", Predictor))# Step 5: Optional — print full tablekable(rate_ratio_table_all, caption ="Unified NB Regression Results with 25–44 as Age Baseline")
Unified NB Regression: Sex, Race, and Age at COVID Onset
All three forest plots (sex, race, and age disparities at COVID onset) are derived from a single unified Negative Binomial regression model that includes interaction terms for sex, race/ethnicity, and age category with COVID onset. This ensures consistency across plots and allows direct comparison of disparities. The accompanying table provides the exact rate ratios, confidence intervals, and percentage changes for each group.
Title: Forest Plot of Sex Disparities in Mortality at COVID Onset
Definition: This analysis uses a Negative Binomial regression with an interaction term between sex and COVID onset. The interaction shows whether the increase in deaths at COVID onset was different for males and femalescompared to the baseline group.
🔹 Why Interaction Matters
Without interaction: COVID onset is treated as a single effect across all sexes.
With interaction: We can see whether males and females experienced different mortality increases at the start of the pandemic.
🔹 How to Read the Plot
Dots = estimated rate ratio for each sex group at COVID onset.
Error bars = 95% confidence intervals.
Dashed line at 1 = no difference relative to baseline.
Labels (%) = percentage change in deaths at COVID onset compared to baseline sex group.
Example: “+20%” means deaths increased by 20% in that sex group.
“–10%” means deaths decreased by 10% relative to baseline.
🔹 Interpretation
The baseline sex group (depending on coding, often Male or Female) shows the reference effect of COVID onset (~25% increase overall).
If sexFemale:covid_onset plots above 1, it means females experienced a larger increase in deaths at COVID onset compared to baseline.
If sexMale:covid_onset plots below 1, it means males experienced a smaller increase in deaths relative to baseline.
Confidence intervals tell us whether these differences are statistically significant.
🔹 Key Takeaway
COVID onset did not affect males and females equally.
The forest plot makes disparities clear: one sex group may have borne a greater mortality burden at the start of the pandemic.
This visualization bridges the gap between statistical output (rate ratios) and real‑world interpretation (percentage changes).
In short: The forest plot shows that sex disparities exist in how mortality counts changed at COVID onset. It provides a clear, visual summary of whether males or females experienced larger increases in deaths, with percentage labels making the impact easy to understand.
library(MASS)library(broom)library(dplyr)library(ggplot2)# Step 1: Fit NB regression with sex × covid_onset interactionnb_model_sex_interact <- MASS::glm.nb( total_deaths ~ sex*covid_onset + race_ethnicity + age_category + year + fentanyl_surge + narcan_distribution + narcan_vending + otc_narcan + vaccine_rollout + booster_campaigns,data = mortality_summary)# Step 2: Tidy output with exponentiationtidy_nb_sex <- broom::tidy(nb_model_sex_interact, conf.int =TRUE, exponentiate =TRUE)# Step 3: Format tablerate_ratio_table_sex <- tidy_nb_sex %>% dplyr::select(term, estimate, conf.low, conf.high, p.value) %>% dplyr::rename(Predictor = term,RateRatio = estimate,LowerCI = conf.low,UpperCI = conf.high,PValue = p.value ) %>% dplyr::mutate(RateRatio =round(RateRatio, 3),LowerCI =round(LowerCI, 3),UpperCI =round(UpperCI, 3),PValue =signif(PValue, 3),PercentChange =round((RateRatio -1) *100, 1) # convert RR to % change )# Step 4: Filter for sex × covid_onset interaction termssex_covid_effects <- rate_ratio_table_sex %>% dplyr::filter(grepl("sex.*:covid_onset", Predictor))# Step 5: Forest plot with percentage labelsggplot(sex_covid_effects, aes(x = Predictor, y = RateRatio)) +geom_point(color ="darkblue", size =3) +geom_errorbar(aes(ymin = LowerCI, ymax = UpperCI), width =0.2, color ="gray40") +geom_text(aes(label =paste0(PercentChange, "%")), hjust =-0.2, vjust =0.5, size =3.5, color ="black") +geom_hline(yintercept =1, linetype ="dashed", color ="black") +coord_flip() +labs(title ="Sex Disparities in Mortality at COVID Onset",x ="Sex",y ="Rate Ratio (COVID Onset Effect)" ) +theme_minimal()
Title: Forest Plot of Age Disparities in Mortality at COVID Onset
Definition: This analysis uses a Negative Binomial regression with an interaction term between age category and COVID onset. The interaction shows whether the increase in deaths at COVID onset was different across age groupscompared to the baseline age category.
🔹 Why Interaction Matters
Without interaction: COVID onset is treated as a single effect across all ages.
With interaction: We can see whether younger vs. older age groups experienced different mortality increases at the start of the pandemic.
🔹 How to Read the Plot
Dots = estimated rate ratio for each age group at COVID onset.
Error bars = 95% confidence intervals.
Dashed line at 1 = no difference relative to baseline.
Labels (%) = percentage change in deaths at COVID onset compared to baseline age group.
Example: “+30%” means deaths increased by 30% in that age group.
“–10%” means deaths decreased by 10% relative to baseline.
🔹 Interpretation
The baseline age group (often the youngest category, depending on coding) shows the reference effect of COVID onset (~25% increase overall).
Older age groups (45–64, 65+) may plot well above 1, showing disproportionately higher increases in deaths at COVID onset.
Younger age groups (5–14, 15–24) may plot closer to or below 1, showing smaller relative increases compared to baseline.
Confidence intervals indicate whether these differences are statistically significant.
🔹 Key Takeaway
COVID onset did not affect all age groups equally.
The forest plot makes disparities clear: older populations bore a greater mortality burden, while younger groups experienced smaller relative increases.
This visualization bridges the gap between statistical output (rate ratios) and real‑world interpretation (percentage changes).
In short: The forest plot shows that age disparities exist in how mortality counts changed at COVID onset. It provides a clear, visual summary of which age groups experienced larger increases in deaths, with percentage labels making the impact easy to understand.
library(MASS)library(broom)library(dplyr)library(ggplot2)# Step 1: Fit NB regression with age × covid_onset interactionnb_model_age_interact <- MASS::glm.nb( total_deaths ~ age_category*covid_onset + sex + race_ethnicity + year + fentanyl_surge + narcan_distribution + narcan_vending + otc_narcan + vaccine_rollout + booster_campaigns,data = mortality_summary)# Step 2: Tidy output with exponentiationtidy_nb_age <- broom::tidy(nb_model_age_interact, conf.int =TRUE, exponentiate =TRUE)# Step 3: Format tablerate_ratio_table_age <- tidy_nb_age %>% dplyr::select(term, estimate, conf.low, conf.high, p.value) %>% dplyr::rename(Predictor = term,RateRatio = estimate,LowerCI = conf.low,UpperCI = conf.high,PValue = p.value ) %>% dplyr::mutate(RateRatio =round(RateRatio, 3),LowerCI =round(LowerCI, 3),UpperCI =round(UpperCI, 3),PValue =signif(PValue, 3),PercentChange =round((RateRatio -1) *100, 1) # convert RR to % change )# Step 4: Filter for age × covid_onset interaction termsage_covid_effects <- rate_ratio_table_age %>% dplyr::filter(grepl("age_category.*:covid_onset", Predictor))# Step 5: Forest plot with percentage labelsggplot(age_covid_effects, aes(x = Predictor, y = RateRatio)) +geom_point(color ="darkgreen", size =3) +geom_errorbar(aes(ymin = LowerCI, ymax = UpperCI), width =0.2, color ="gray40") +geom_text(aes(label =paste0(PercentChange, "%")), hjust =-0.2, vjust =0.5, size =3.5, color ="black") +geom_hline(yintercept =1, linetype ="dashed", color ="black") +coord_flip() +labs(title ="Age Disparities in Mortality at COVID Onset",x ="Age Category",y ="Rate Ratio (COVID Onset Effect)" ) +theme_minimal()
Title: Forest Plot of Racial Disparities in Mortality at COVID Onset
Definition: This forest plot shows rate ratios with confidence intervals for each racial/ethnic group at COVID onset, along with percentage change labels. The labels translate statistical results into real‑world terms, making disparities easier to interpret.
🔹 How to Read the Plot
Dots = estimated rate ratio for each racial group at COVID onset.
Error bars = 95% confidence intervals.
Dashed line at 1 = no difference relative to baseline.
Labels (%) = percentage change in deaths at COVID onset compared to baseline.
Example: A label of “+25%” means deaths increased by 25% in that group.
A label of “–10%” means deaths decreased by 10% relative to baseline.
🔹 Interpretation
Baseline race group (often White NH, depending on coding) shows the reference effect of COVID onset (~25% increase overall).
Black and Hispanic groups may show labels above +25%, indicating disproportionately higher increases in deaths at COVID onset.
Asian/PI groups may show smaller or even negative percentage changes, indicating lower relative increasescompared to baseline.
Multiracial groups may also differ, depending on the dataset.
🔹 Key Takeaway
COVID onset did not affect all racial/ethnic groups equally.
The forest plot with percentage labels makes disparities clear: some groups experienced larger mortality burdens, while others experienced smaller relative increases.
This visualization bridges the gap between statistical output (rate ratios) and real‑world interpretation (percentage changes).
In short: The enhanced forest plot shows both the statistical significance (rate ratios and confidence intervals) and the practical impact (percentage changes in deaths) side‑by‑side. This makes racial disparities at COVID onset immediately clear to both technical and non‑technical audiences.
5 Conclusion
This study examined mortality trends across sex, race/ethnicity, and age categories in the context of opioid and COVID‑19 interventions. Visualizations revealed sharp increases in deaths following the 2014 fentanyl surge and the 2020 onset of COVID, with persistent disparities across demographic groups. Vaccination rollout stabilized mortality among older adults but showed uneven protective effects across racial and ethnic subgroups. Opioid interventions, including Narcan distribution, vending machines, and OTC availability, produced mixed results, with OTC Narcan emerging as the most consistent protective measure.
Regression analyses confirmed that demographics are the dominant drivers of mortality variation. Females consistently experienced fewer deaths than males, Asian/PI and Hispanic groups showed protective effects relative to White (NH), while Black populations faced excess mortality. Age was the strongest predictor, with older groups experiencing dramatically higher death counts. Linear regression highlighted the limits of simple models for count data, while Poisson regression quantified relative risks and intervention impacts. Negative binomial regression provided the most reliable inference, handling overdispersion and confirming significant effects for OTC Narcan (protective) and COVID onset (harmful). Forest plots visually reinforced these findings, showing demographics far outweighing interventions in their impact.
Equity‑focused interaction models revealed that vaccination effectiveness varied across race/ethnicity, with weaker protective effects for Hispanic, Asian/PI, and Multiracial groups, while sex and age disparities remained largely unchanged. Opioid interventions showed some evidence of narrowing disparities for Multiracial groups and borderline narrowing for younger and older age categories, but overall impacts were uneven. Age‑specific analyses of Narcan interventions further demonstrated that benefits were strongest among adults aged 25–44, weaker in older groups, and similar in younger groups, contradicting the hypothesis of greater protection for youth.
Taken together, these findings underscore three central points:
Demographic disparities drive mortality patterns more than interventions, with race/ethnicity and age showing the most pronounced inequities.
Intervention impacts are uneven, with OTC Narcan and COVID vaccination emerging as the clearest protective measures, but with subgroup differences that limit equity gains.
Nuanced modeling approaches are essential — ITS, Poisson, and negative binomial regressions reveal patterns that simple linear models cannot, providing a more accurate picture of intervention effectiveness and equity impacts.
Future work should refine subgroup interaction models and expand equity‑focused evaluation to identify where interventions most effectively reduce disparities. Public health strategies must be tailored to demographic realities, ensuring that policies not only reduce overall mortality but also close gaps across sex, race/ethnicity, and age.